add the Mut<> type
This commit is contained in:
@@ -50,7 +50,7 @@ pub mod prelude {
|
||||
pub use crate::time::dur;
|
||||
pub use crate::fnv1::*;
|
||||
pub use crate::pstruct::struct_offset;
|
||||
pub use crate::pod::Pod;
|
||||
pub use crate::pod::{Pod,Mut};
|
||||
}
|
||||
|
||||
pub mod public {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use core::mem::MaybeUninit;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
|
||||
/// Plain Old data type
|
||||
pub trait Pod: Copy + 'static {
|
||||
@@ -17,4 +18,33 @@ primitive!(i8, i16, i32, i64, i128);
|
||||
primitive!(f32, f64, usize, isize);
|
||||
primitive!(());
|
||||
|
||||
impl<const LEN: usize, T: Pod> Pod for [T;LEN] {}
|
||||
impl<const LEN: usize, T: Pod> Pod for [T;LEN] {}
|
||||
|
||||
|
||||
/// Type with unsafe interior mutability
|
||||
/// but also with Send+Sync
|
||||
///
|
||||
/// internally just wraps UnsafeCell
|
||||
#[repr(transparent)]
|
||||
pub struct Mut<T>(core::cell::UnsafeCell<T>);
|
||||
unsafe impl<T> Sync for Mut<T> {}
|
||||
unsafe impl<T> Send for Mut<T> {}
|
||||
|
||||
impl<T: Sized> Mut<T> {
|
||||
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<T: Pod> Mut<T> {
|
||||
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<T> Deref for Mut<T> {
|
||||
type Target = core::cell::UnsafeCell<T>;
|
||||
fn deref(&self) -> &Self::Target { &self.0 }
|
||||
}
|
||||
Reference in New Issue
Block a user