atomic_types/
traits.rs

1pub use core::sync::atomic::Ordering;
2
3/// Trait describing types that are atomically sized, i.e. it's possible to perform
4/// atomic operations on them.
5pub trait AtomicallySized: Sized {
6    type AtomicVersion: Atomic<Self>;
7}
8
9/// Trait describing an atomic version of an atomically-sized type.
10pub trait Atomic<T>: Send + Sync + Sized
11where
12    T: Sized,
13{
14    /// Generate a reference to the atomic version of this type using its normal
15    /// version.
16    unsafe fn from_raw(val: &T) -> &Self {
17        &*(val as *const T as *const Self)
18    }
19
20    fn new(val: T) -> Self;
21
22    fn store(&self, val: T, order: Ordering);
23
24    fn load(&self, order: Ordering) -> T;
25
26    fn swap(&self, val: T, order: Ordering) -> T;
27
28    fn get_mut(&mut self) -> &mut T;
29
30    fn into_inner(self) -> T;
31
32    fn compare_and_swap(&self, current: T, new: T, order: Ordering) -> T;
33
34    fn compare_exchange(
35        &self,
36        current: T,
37        new: T,
38        success: Ordering,
39        failure: Ordering,
40    ) -> Result<T, T>;
41
42    fn compare_exchange_weak(
43        &self,
44        current: T,
45        new: T,
46        success: Ordering,
47        failure: Ordering,
48    ) -> Result<T, T>;
49}
50
51/// Trait describing structs that implement atomic integer operations.
52///
53/// Makes generic containers a little easier.
54pub trait AtomicNumeric<T>: Atomic<T> {
55    fn fetch_add(&self, val: T, order: Ordering) -> T;
56    fn fetch_sub(&self, val: T, order: Ordering) -> T;
57}
58
59/// Trait describing structs that implement atomic bitwise operations.
60///
61/// Makes generic containers a little easier.
62pub trait AtomicBitwise<T>: Atomic<T> {
63    fn fetch_and(&self, val: T, order: Ordering) -> T;
64    fn fetch_or(&self, val: T, order: Ordering) -> T;
65    fn fetch_xor(&self, val: T, order: Ordering) -> T;
66    fn fetch_nand(&self, val: T, order: Ordering) -> T;
67}