1use std::sync::atomic::{AtomicI64, AtomicU32, AtomicU64, AtomicUsize, Ordering};
6
7pub trait AsAtomic: Copy {
9 type Atomic: AtomicOperations<Self>;
10 const ONE: Self;
11}
12
13pub trait AtomicOperations<T> {
15 fn new(value: T) -> Self;
16 fn load(&self, order: Ordering) -> T;
17 fn store(&self, value: T, order: Ordering);
18 fn fetch_add(&self, value: T, order: Ordering) -> T;
19}
20
21macro_rules! impl_atomic {
22 ($ty:ty, $atomic:ty) => {
23 impl AsAtomic for $ty {
24 type Atomic = $atomic;
25 const ONE: $ty = 1;
26 }
27
28 impl AtomicOperations<$ty> for $atomic {
29 fn new(value: $ty) -> Self {
30 <$atomic>::new(value)
31 }
32 fn load(&self, order: Ordering) -> $ty {
33 self.load(order)
34 }
35 fn store(&self, value: $ty, order: Ordering) {
36 self.store(value, order)
37 }
38 fn fetch_add(&self, value: $ty, order: Ordering) -> $ty {
39 self.fetch_add(value, order)
40 }
41 }
42 };
43}
44
45impl_atomic!(i64, AtomicI64);
46impl_atomic!(u64, AtomicU64);
47impl_atomic!(u32, AtomicU32);
48impl_atomic!(usize, AtomicUsize);