ntex_util/channel/
cell.rs

1use std::{cell::UnsafeCell, fmt, rc::Rc};
2
3pub(super) struct Cell<T> {
4    inner: Rc<UnsafeCell<T>>,
5}
6
7impl<T> Clone for Cell<T> {
8    fn clone(&self) -> Self {
9        Self {
10            inner: self.inner.clone(),
11        }
12    }
13}
14
15impl<T: fmt::Debug> fmt::Debug for Cell<T> {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        self.inner.fmt(f)
18    }
19}
20
21impl<T> Cell<T> {
22    pub(super) fn new(inner: T) -> Self {
23        Self {
24            inner: Rc::new(UnsafeCell::new(inner)),
25        }
26    }
27
28    pub(super) fn strong_count(&self) -> usize {
29        Rc::strong_count(&self.inner)
30    }
31
32    pub(super) fn get_ref(&self) -> &T {
33        unsafe { &*self.inner.as_ref().get() }
34    }
35
36    #[allow(clippy::mut_from_ref)]
37    pub(super) fn get_mut(&self) -> &mut T {
38        unsafe { &mut *self.inner.as_ref().get() }
39    }
40}