binius_utils/
thread_local_mut.rs1use std::cell::UnsafeCell;
4
5use thread_local::ThreadLocal;
6
7#[derive(Debug, Default)]
14pub struct ThreadLocalMut<T: Send>(ThreadLocal<UnsafeCell<T>>);
15
16impl<T: Send> ThreadLocalMut<T> {
17 pub fn new() -> Self {
18 Self(ThreadLocal::new())
19 }
20
21 #[inline]
22 pub fn with_mut<U>(&self, init: impl FnOnce() -> T, run_scope: impl FnOnce(&mut T) -> U) -> U {
23 let data = self.0.get_or(|| UnsafeCell::new(init()));
24 run_scope(unsafe { data.get().as_mut().unwrap_unchecked() })
25 }
26}