2018-10-02 22:18:07 -07:00
|
|
|
//! Custom cell impl
|
|
|
|
|
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
use std::fmt;
|
2019-12-11 11:28:09 +06:00
|
|
|
use std::rc::Rc;
|
2018-10-02 22:18:07 -07:00
|
|
|
|
2018-12-03 17:46:25 -08:00
|
|
|
pub(crate) struct Cell<T> {
|
2019-12-05 01:36:31 +06:00
|
|
|
pub(crate) inner: Rc<UnsafeCell<T>>,
|
|
|
|
}
|
|
|
|
|
2018-10-02 22:18:07 -07:00
|
|
|
impl<T> Clone for Cell<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: fmt::Debug> fmt::Debug for Cell<T> {
|
2019-12-02 22:30:09 +06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-10-02 22:18:07 -07:00
|
|
|
self.inner.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Cell<T> {
|
2019-12-11 11:28:09 +06:00
|
|
|
pub(crate) fn new(inner: T) -> Self {
|
2018-10-02 22:18:07 -07:00
|
|
|
Self {
|
|
|
|
inner: Rc::new(UnsafeCell::new(inner)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:28:09 +06:00
|
|
|
pub(crate) fn strong_count(&self) -> usize {
|
|
|
|
Rc::strong_count(&self.inner)
|
2019-12-05 01:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:28:09 +06:00
|
|
|
pub(crate) fn get_ref(&self) -> &T {
|
2018-10-02 22:18:07 -07:00
|
|
|
unsafe { &*self.inner.as_ref().get() }
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:28:09 +06:00
|
|
|
pub(crate) fn get_mut(&mut self) -> &mut T {
|
2018-10-02 22:18:07 -07:00
|
|
|
unsafe { &mut *self.inner.as_ref().get() }
|
|
|
|
}
|
2019-12-05 01:36:31 +06:00
|
|
|
|
2019-12-11 12:42:07 +06:00
|
|
|
#[allow(clippy::mut_from_ref)]
|
2019-12-11 11:28:09 +06:00
|
|
|
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
|
|
|
|
&mut *self.inner.as_ref().get()
|
2019-12-05 01:36:31 +06:00
|
|
|
}
|
|
|
|
}
|