2019-11-14 18:38:24 +06:00
|
|
|
//! Custom cell impl, internal use only
|
2018-12-09 09:56:23 -08:00
|
|
|
use std::{cell::UnsafeCell, fmt, rc::Rc};
|
|
|
|
|
|
|
|
pub(crate) struct Cell<T> {
|
|
|
|
inner: Rc<UnsafeCell<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for Cell<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: fmt::Debug> fmt::Debug for Cell<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Cell<T> {
|
|
|
|
pub(crate) fn new(inner: T) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Rc::new(UnsafeCell::new(inner)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:28:07 +06:00
|
|
|
pub(crate) fn get_ref(&self) -> &T {
|
|
|
|
unsafe { &*self.inner.as_ref().get() }
|
|
|
|
}
|
|
|
|
|
2018-12-09 09:56:23 -08:00
|
|
|
pub(crate) fn get_mut(&mut self) -> &mut T {
|
|
|
|
unsafe { &mut *self.inner.as_ref().get() }
|
|
|
|
}
|
2019-05-12 06:03:50 -07:00
|
|
|
|
2019-08-17 05:15:51 +09:00
|
|
|
#[allow(clippy::mut_from_ref)]
|
2019-05-12 06:03:50 -07:00
|
|
|
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
|
|
|
|
&mut *self.inner.as_ref().get()
|
|
|
|
}
|
2018-12-09 09:56:23 -08:00
|
|
|
}
|