1
0
Fork 0
forked from wry/wry

atomic_enum: add new utility

This commit is contained in:
Julian Orth 2026-03-07 14:35:17 +01:00
parent 8fc9ead21e
commit be0782f5d2
2 changed files with 44 additions and 0 deletions

View file

@ -2,6 +2,7 @@ pub mod activation_token;
pub mod array;
pub mod array_to_tuple;
pub mod asyncevent;
pub mod atomic_enum;
pub mod binary_search_map;
pub mod bindings;
pub mod bitfield;

43
src/utils/atomic_enum.rs Normal file
View file

@ -0,0 +1,43 @@
use {
linearize::Linearize,
std::{
marker::PhantomData,
sync::atomic::{AtomicUsize, Ordering},
},
};
pub struct AtomicEnum<T> {
v: AtomicUsize,
_phantom: PhantomData<T>,
}
impl<T> Default for AtomicEnum<T>
where
T: Default + Linearize + Copy,
{
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> AtomicEnum<T>
where
T: Linearize + Copy,
{
pub fn new(t: T) -> Self {
Self {
v: AtomicUsize::new(t.linearize()),
_phantom: Default::default(),
}
}
#[expect(dead_code)]
pub fn load(&self, ordering: Ordering) -> T {
unsafe { T::from_linear_unchecked(self.v.load(ordering)) }
}
#[expect(dead_code)]
pub fn store(&self, t: T, ordering: Ordering) {
self.v.store(t.linearize(), ordering);
}
}