spinning_top

image of a spinning top

Build Status Docs.rs Badge

A simple spinlock crate based on the abstractions provided by [lock_api].

Example

First, import the crate as a dependency in your Cargo.toml. Then you can use it in the following way:

```rust use spinning_top::Spinlock;

fn main() { // Wrap some data in a spinlock let data = String::from("Hello"); let spinlock = Spinlock::new(data); makeuppercase(&spinlock); // only pass a shared reference // We have ownership of the spinlock, so we can extract the data without locking // Note: this consumes the spinlock let data = spinlock.intoinner(); asserteq!(data.asstr(), "HELLO"); }

fn makeuppercase(spinlock: &Spinlock) { // Lock the spinlock to get a mutable reference to the data let mut lockeddata = spinlock.lock(); asserteq!(lockeddata.asstr(), "Hello"); lockeddata.makeasciiuppercase();

// the lock is automatically freed at the end of the scope

} ```

The nightly Feature

On Rust nightly, the nightly feature of this crate can be enabled to make the Spinlock::new function a const function. This makes the Spinlock type usable in statics:

```rust use spinning_top::Spinlock;

static DATA: Spinlock = Spinlock::new(0);

fn main() { let mut data = DATA.lock(); data += 1; assert_eq!(data, 1); } ```

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.