use crate::rt;
#[derive(Debug)]
pub struct UnsafeCell<T> {
state: rt::Cell,
data: std::cell::UnsafeCell<T>,
}
impl<T> UnsafeCell<T> {
#[cfg_attr(loom_nightly, track_caller)]
pub fn new(data: T) -> UnsafeCell<T> {
let state = rt::Cell::new(location!());
UnsafeCell {
state,
data: std::cell::UnsafeCell::new(data),
}
}
#[cfg_attr(loom_nightly, track_caller)]
pub fn with<F, R>(&self, f: F) -> R
where
F: FnOnce(*const T) -> R,
{
self.state
.with(location!(), || f(self.data.get() as *const T))
}
#[cfg_attr(loom_nightly, track_caller)]
pub fn with_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(*mut T) -> R,
{
self.state.with_mut(location!(), || f(self.data.get()))
}
}
impl<T: Default> Default for UnsafeCell<T> {
fn default() -> UnsafeCell<T> {
UnsafeCell::new(Default::default())
}
}
impl<T> From<T> for UnsafeCell<T> {
fn from(src: T) -> UnsafeCell<T> {
UnsafeCell::new(src)
}
}