use core::cell::UnsafeCell; use core::mem::MaybeUninit; use core::ptr; pub(crate) struct UninitCell(MaybeUninit>); impl UninitCell { pub const fn uninit() -> Self { Self(MaybeUninit::uninit()) } pub unsafe fn as_mut_ptr(&self) -> *mut T { (*self.0.as_ptr()).get() } #[allow(clippy::mut_from_ref)] pub unsafe fn as_mut(&self) -> &mut T { &mut *self.as_mut_ptr() } #[inline(never)] pub unsafe fn write_in_place(&self, func: impl FnOnce() -> T) { ptr::write(self.as_mut_ptr(), func()) } pub unsafe fn drop_in_place(&self) { ptr::drop_in_place(self.as_mut_ptr()) } } unsafe impl Sync for UninitCell {} #[repr(transparent)] pub struct SyncUnsafeCell { value: UnsafeCell, } unsafe impl Sync for SyncUnsafeCell {} impl SyncUnsafeCell { #[inline] pub const fn new(value: T) -> Self { Self { value: UnsafeCell::new(value), } } pub unsafe fn set(&self, value: T) { *self.value.get() = value; } pub unsafe fn get(&self) -> T where T: Copy, { *self.value.get() } }