#atomic #ordering #traits #fetch #operations #generic #generics

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

#210 in Algorithms

Download history 5416/week @ 2023-11-20 5684/week @ 2023-11-27 4526/week @ 2023-12-04 4533/week @ 2023-12-11 6170/week @ 2023-12-18 2569/week @ 2023-12-25 4438/week @ 2024-01-01 4871/week @ 2024-01-08 6298/week @ 2024-01-15 6641/week @ 2024-01-22 5439/week @ 2024-01-29 4808/week @ 2024-02-05 6088/week @ 2024-02-12 5906/week @ 2024-02-19 7112/week @ 2024-02-26 5272/week @ 2024-03-04

24,752 downloads per month
Used in 21 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–29MB
~360K SLoC