#atomic #atom #ordering #cas #generic

atomig

Generic and convenient std atomics via Atomic<T>. Can be used with many primitive types (including floats) and with custom types.

5 unstable releases

0.3.2 Oct 2, 2021
0.3.1 Jun 18, 2021
0.3.0 Jun 18, 2021
0.2.0 Jul 30, 2020
0.1.0 Jul 24, 2019

#34 in Concurrency

Download history 1170/week @ 2021-06-30 1323/week @ 2021-07-07 1142/week @ 2021-07-14 1304/week @ 2021-07-21 1062/week @ 2021-07-28 1108/week @ 2021-08-04 1334/week @ 2021-08-11 1631/week @ 2021-08-18 1446/week @ 2021-08-25 1270/week @ 2021-09-01 1658/week @ 2021-09-08 2212/week @ 2021-09-15 1085/week @ 2021-09-22 1471/week @ 2021-09-29 2040/week @ 2021-10-06 1644/week @ 2021-10-13

5,884 downloads per month
Used in 2 crates (via cargo-limit)

MIT/Apache

64KB
789 lines

Atomig: generic and convenient std atomics

CI status of master Crates.io Version docs.rs

Offers Atomic<T> that can be used with primitive and custom types. However, it only works with types that can actually use atomic operations: a lock-based fallback for other types is not used! This crate is based on std's atomics and therefore does not contain any unsafe code! This crate also does not have any dependencies by default. If you enable the serde feature, then this crate will depend on serde and Serialize / Deserialize will be implemented for Atomic<T> when appropriate, using sequentially-consistent ordering.

Simple example with primitive types:

use atomig::{Atomic, Ordering};

let x = Atomic::new(27); // `Atomic<i32>`
x.store(39, Ordering::SeqCst);

This works with almost all primitive types, including f32, f64 and char, but also with types like std::ptr::NonNull and std::num::NonZero.

You can automatically derive Atom for your own enum or struct types to use them in Atomic<T>. There are some limitations, however.

// Requires the 'derive' feature:
//     atomig = { version = "_", features = ["derive"] }
use atomig::{Atom, Atomic, Ordering};

#[derive(Atom)]
#[repr(u8)]
enum Animal { Dog, Cat, Fox }

let animal = Atomic::new(Animal::Cat);
animal.store(Animal::Fox, Ordering::SeqCst);

#[derive(Atom)]
struct Port(u16);

let port = Atomic::new(Port(80));
port.store(Port(8080), Ordering::SeqCst);

For more examples and information see the documentation.



License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–315KB