use core::mem::MaybeUninit; use core::ops::{Deref, DerefMut}; /// Plain Old data type pub trait Pod: Copy + 'static { fn uninit() -> Self { unsafe { MaybeUninit::uninit().assume_init() } } fn zeroed() -> Self { unsafe { MaybeUninit::zeroed().assume_init() } } } macro_rules! primitive { ($($p:ty),*) => { $(impl Pod for $p {})* } } primitive!(u8, u16, u32, u64, u128); primitive!(i8, i16, i32, i64, i128); primitive!(f32, f64, usize, isize); primitive!(()); impl Pod for [T;LEN] {} /// Type with unsafe interior mutability /// but also with Send+Sync /// /// internally just wraps UnsafeCell #[repr(transparent)] pub struct Mut(core::cell::UnsafeCell); unsafe impl Sync for Mut {} unsafe impl Send for Mut {} impl Mut { pub const fn new(value: T) -> Self { Self(core::cell::UnsafeCell::new(value)) } pub const fn as_ptr(&self) -> *mut T { self.0.get() } } impl Mut { pub fn set(&self, value: T) { unsafe { core::ptr::write(self.as_ptr(), value) } } pub fn get(&self) -> T { unsafe { core::ptr::read(self.as_ptr()) } } } impl Mut> { pub const fn zeroed() -> Self { Self(core::cell::UnsafeCell::new(MaybeUninit::zeroed())) } pub const fn uninit() -> Self { Self(core::cell::UnsafeCell::new(MaybeUninit::uninit())) } } impl Deref for Mut { type Target = core::cell::UnsafeCell; fn deref(&self) -> &Self::Target { &self.0 } }