binius_utils/
thread_local_mut.rsuse std::cell::UnsafeCell;
use thread_local::ThreadLocal;
#[derive(Debug, Default)]
pub struct ThreadLocalMut<T: Send>(ThreadLocal<UnsafeCell<T>>);
impl<T: Send> ThreadLocalMut<T> {
#[allow(clippy::missing_const_for_fn)]
pub fn new() -> Self {
Self(ThreadLocal::new())
}
#[inline]
pub fn with_mut<U>(&self, init: impl FnOnce() -> T, run_scope: impl FnOnce(&mut T) -> U) -> U {
let data = self.0.get_or(|| UnsafeCell::new(init()));
run_scope(unsafe { data.get().as_mut().unwrap_unchecked() })
}
}