https://github.com/flier/rust-atomic-traits
The traits for generic atomic operations in Rust.
https://github.com/flier/rust-atomic-traits
Last synced: over 1 year ago
JSON representation
The traits for generic atomic operations in Rust.
- Host: GitHub
- URL: https://github.com/flier/rust-atomic-traits
- Owner: flier
- License: apache-2.0
- Created: 2019-06-04T06:17:59.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-02-12T12:00:05.000Z (over 2 years ago)
- Last Synced: 2024-10-13T20:49:40.954Z (almost 2 years ago)
- Language: Rust
- Homepage:
- Size: 61.5 KB
- Stars: 8
- Watchers: 3
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# atomic-traits [](https://crates.io/crates/atomic-traits) [](https://docs.rs/atomic-traits/) [](https://github.com/flier/rust-atomic-traits/actions/workflows/ci.yaml) [](https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html)
The traits for generic atomic operations in Rust.
## Compatibility
The crate is tested for stable and nightly compiler.
Current MSRV is `1.34.0`.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
atomic-traits = "0.4"
```
and this to your crate root:
```rust
extern crate atomic_traits;
```
## Example
```rust
extern crate num_traits;
extern crate atomic_traits;
use std::sync::atomic::{AtomicUsize, Ordering};
use num_traits::One;
use atomic_traits::{Atomic, NumOps, fetch};
#[derive(Debug, Default)]
pub struct RefCnt(T);
impl RefCnt
where
T: Atomic + NumOps + Default,
::Type: One
{
pub fn inc(&self) -> ::Type {
self.0.fetch_add(::Type::one(), Ordering::Acquire)
}
pub fn dec(&self) -> ::Type {
self.0.fetch_sub(::Type::one(), Ordering::Release)
}
pub fn val(&self) -> ::Type {
self.0.load(Ordering::SeqCst)
}
}
let refcnt = RefCnt::::default();
assert_eq!(refcnt.inc(), 0);
assert_eq!(refcnt.dec(), 1);
assert_eq!(refcnt.val(), 0);
```