atomic_enum: add new utility
This commit is contained in:
parent
8fc9ead21e
commit
be0782f5d2
2 changed files with 44 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ pub mod activation_token;
|
||||||
pub mod array;
|
pub mod array;
|
||||||
pub mod array_to_tuple;
|
pub mod array_to_tuple;
|
||||||
pub mod asyncevent;
|
pub mod asyncevent;
|
||||||
|
pub mod atomic_enum;
|
||||||
pub mod binary_search_map;
|
pub mod binary_search_map;
|
||||||
pub mod bindings;
|
pub mod bindings;
|
||||||
pub mod bitfield;
|
pub mod bitfield;
|
||||||
|
|
|
||||||
43
src/utils/atomic_enum.rs
Normal file
43
src/utils/atomic_enum.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue