Crates.io | atomic-traits |
lib.rs | atomic-traits |
version | 0.4.0 |
source | src |
created_at | 2019-06-04 08:27:00.95921 |
updated_at | 2024-02-12 11:53:01.331264 |
description | The traits for generic atomic operations |
homepage | https://github.com/flier/rust-atomic-traits |
repository | |
max_upload_size | |
id | 138821 |
size | 61,409 |
The traits for generic atomic operations in Rust.
The crate is tested for stable and nightly compiler.
Current MSRV is 1.34.0
.
Add this to your Cargo.toml
:
[dependencies]
atomic-traits = "0.4"
and this to your crate root:
extern crate atomic_traits;
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>(T);
impl<T> RefCnt<T>
where
T: Atomic + NumOps + Default,
<T as Atomic>::Type: One
{
pub fn inc(&self) -> <T as Atomic>::Type {
self.0.fetch_add(<T as Atomic>::Type::one(), Ordering::Acquire)
}
pub fn dec(&self) -> <T as Atomic>::Type {
self.0.fetch_sub(<T as Atomic>::Type::one(), Ordering::Release)
}
pub fn val(&self) -> <T as Atomic>::Type {
self.0.load(Ordering::SeqCst)
}
}
let refcnt = RefCnt::<AtomicUsize>::default();
assert_eq!(refcnt.inc(), 0);
assert_eq!(refcnt.dec(), 1);
assert_eq!(refcnt.val(), 0);