2020-12-26 16:42:44 +01:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::mem::MaybeUninit;
|
|
|
|
use core::ptr;
|
|
|
|
|
|
|
|
pub(crate) struct UninitCell<T>(MaybeUninit<UnsafeCell<T>>);
|
|
|
|
impl<T> UninitCell<T> {
|
|
|
|
pub const fn uninit() -> Self {
|
|
|
|
Self(MaybeUninit::uninit())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn as_mut_ptr(&self) -> *mut T {
|
|
|
|
(*self.0.as_ptr()).get()
|
|
|
|
}
|
|
|
|
|
2021-10-18 00:55:43 +02:00
|
|
|
#[allow(clippy::mut_from_ref)]
|
2020-12-26 16:42:44 +01:00
|
|
|
pub unsafe fn as_mut(&self) -> &mut T {
|
|
|
|
&mut *self.as_mut_ptr()
|
|
|
|
}
|
|
|
|
|
2023-09-14 05:03:38 +02:00
|
|
|
#[inline(never)]
|
|
|
|
pub unsafe fn write_in_place(&self, func: impl FnOnce() -> T) {
|
|
|
|
ptr::write(self.as_mut_ptr(), func())
|
2020-12-26 16:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn drop_in_place(&self) {
|
|
|
|
ptr::drop_in_place(self.as_mut_ptr())
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 22:20:51 +01:00
|
|
|
|
|
|
|
unsafe impl<T> Sync for UninitCell<T> {}
|
|
|
|
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct SyncUnsafeCell<T> {
|
|
|
|
value: UnsafeCell<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Sync> Sync for SyncUnsafeCell<T> {}
|
|
|
|
|
|
|
|
impl<T> SyncUnsafeCell<T> {
|
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
}
|