#atomic #traits

no-std atomic-traits

The traits for generic atomic operations

4 releases (breaking)

Uses old Rust 2015

0.4.0 Feb 12, 2024
0.3.0 May 25, 2021
0.2.0 Jul 24, 2020
0.1.0 Jun 4, 2019

#150 in Algorithms

Download history 6035/week @ 2023-11-02 5715/week @ 2023-11-09 5784/week @ 2023-11-16 5322/week @ 2023-11-23 5888/week @ 2023-11-30 4131/week @ 2023-12-07 5302/week @ 2023-12-14 4875/week @ 2023-12-21 2960/week @ 2023-12-28 4851/week @ 2024-01-04 5983/week @ 2024-01-11 6350/week @ 2024-01-18 6352/week @ 2024-01-25 4694/week @ 2024-02-01 5632/week @ 2024-02-08 5136/week @ 2024-02-15

23,298 downloads per month
Used in 20 crates (5 directly)

MIT/Apache

47KB
657 lines

atomic-traits crate documentation Continuous integration MSRV

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:

[dependencies]
atomic-traits = "0.4"

and this to your crate root:

extern crate atomic_traits;

Example

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);

Dependencies

~0–31MB
~410K SLoC