use std::thread::LocalKey;
use crate::InterruptRefCell;
pub trait LocalKeyExt<T> {
fn with_borrow<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&T) -> R;
fn with_borrow_mut<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&mut T) -> R;
fn set(&'static self, value: T);
fn take(&'static self) -> T
where
T: Default;
fn replace(&'static self, value: T) -> T;
}
impl<T: 'static> LocalKeyExt<T> for LocalKey<InterruptRefCell<T>> {
fn with_borrow<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&T) -> R,
{
self.with(|cell| f(&cell.borrow()))
}
fn with_borrow_mut<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
self.with(|cell| f(&mut cell.borrow_mut()))
}
fn set(&'static self, value: T) {
self.with(|cell| {
*cell.borrow_mut() = value;
});
}
fn take(&'static self) -> T
where
T: Default,
{
self.with(|cell| cell.take())
}
fn replace(&'static self, value: T) -> T {
self.with(|cell| cell.replace(value))
}
}