#atomic #helper #atom #generic #enums #atomig #atomic-t

macro atomig-macro

Helper crate for atomig. Do not use directly, but only through main crate. This helper does not follow semantic versioning!

3 releases (breaking)

0.3.0 Apr 9, 2022
0.2.0 Jul 30, 2020
0.1.0 Jul 24, 2019

#48 in #atom

Download history 161/week @ 2024-07-26 120/week @ 2024-08-02 167/week @ 2024-08-09 84/week @ 2024-08-16 99/week @ 2024-08-23 86/week @ 2024-08-30 89/week @ 2024-09-06 88/week @ 2024-09-13 186/week @ 2024-09-20 132/week @ 2024-09-27 1867/week @ 2024-10-04 3977/week @ 2024-10-11 2286/week @ 2024-10-18 2205/week @ 2024-10-25 3473/week @ 2024-11-01 4601/week @ 2024-11-08

13,715 downloads per month
Used in 9 crates (via atomig)

MIT/Apache

10KB
182 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

~1.5MB
~37K SLoC