Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rust-osdev/spinning_top
A simple spinlock crate based on the abstractions provided by the `lock_api` crate.
https://github.com/rust-osdev/spinning_top
Last synced: 3 days ago
JSON representation
A simple spinlock crate based on the abstractions provided by the `lock_api` crate.
- Host: GitHub
- URL: https://github.com/rust-osdev/spinning_top
- Owner: rust-osdev
- License: apache-2.0
- Created: 2020-02-12T12:21:42.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-19T05:52:22.000Z (about 1 year ago)
- Last Synced: 2024-04-24T08:15:33.904Z (8 months ago)
- Language: Rust
- Size: 103 KB
- Stars: 37
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# spinning_top
[![Build Status](https://github.com/rust-osdev/spinning_top/workflows/Build/badge.svg)](https://github.com/rust-osdev/spinning_top/actions?query=workflow%3ABuild) [![Docs.rs Badge](https://docs.rs/spinning_top/badge.svg)](https://docs.rs/spinning_top/)
A simple spinlock crate based on the abstractions provided by [`lock_api`].
[`lock_api`]: https://docs.rs/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);
make_uppercase(&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.into_inner();
assert_eq!(data.as_str(), "HELLO");
}fn make_uppercase(spinlock: &Spinlock) {
// Lock the spinlock to get a mutable reference to the data
let mut locked_data = spinlock.lock();
assert_eq!(locked_data.as_str(), "Hello");
locked_data.make_ascii_uppercase();// the lock is automatically freed at the end of the scope
}
````Spinlock::new` is 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
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)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.