#![no_std]
use core::cell::UnsafeCell;
use core::fmt;
use core::mem::MaybeUninit;
use core::ops::Deref;
pub struct RoCell<T>(UnsafeCell<MaybeUninit<T>>);
unsafe impl<T: Send> Send for RoCell<T> {}
unsafe impl<T: Sync> Sync for RoCell<T> {}
impl<T> Drop for RoCell<T> {
#[inline]
fn drop(&mut self) {
unsafe { core::ptr::drop_in_place((*self.0.get()).as_mut_ptr()) };
}
}
impl<T> RoCell<T> {
#[inline]
pub const fn new(value: T) -> Self {
RoCell(UnsafeCell::new(MaybeUninit::new(value)))
}
#[inline]
pub const unsafe fn new_uninit() -> Self {
RoCell(UnsafeCell::new(MaybeUninit::uninit()))
}
#[inline]
pub unsafe fn init(this: &Self, value: T) {
core::ptr::write((*this.0.get()).as_mut_ptr(), value);
}
#[inline]
pub unsafe fn replace(this: &Self, value: T) -> T {
core::mem::replace(RoCell::as_mut(this), value)
}
#[inline]
pub unsafe fn as_mut(this: &Self) -> &mut T {
&mut *(*this.0.get()).as_mut_ptr()
}
}
impl<T> Deref for RoCell<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*(*self.0.get()).as_ptr() }
}
}
impl<T: fmt::Debug> fmt::Debug for RoCell<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.deref(), f)
}
}