use crate::Mutex;
pub struct TakeCell<T>(Mutex<Option<T>>);
impl<T> TakeCell<T> {
pub const fn new(init: Option<T>) -> Self {
TakeCell(Mutex::new(init))
}
#[track_caller]
pub fn take(&self) -> T {
self.get().unwrap()
}
#[track_caller]
pub fn get(&self) -> Option<T> {
self.0.lock().take()
}
#[track_caller]
pub fn replace(&self, value: T) -> Option<T> {
self.0.lock().replace(value)
}
#[track_caller]
pub fn put(&self, value: T) {
assert!(self.replace(value).is_none())
}
#[track_caller]
pub fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
f(self.0.lock().as_mut().unwrap())
}
}