state/
shim.rs

1#[cfg(not(loom))] pub use std::sync;
2#[cfg(loom)] pub use loom::sync;
3
4pub mod cell {
5    #[cfg(not(loom))] type Inner<T> = std::cell::UnsafeCell<T>;
6    #[cfg(loom)] type Inner<T> = loom::cell::UnsafeCell<T>;
7
8    #[derive(Debug)]
9    pub struct UnsafeCell<T>(Inner<T>);
10
11    impl<T> UnsafeCell<T> {
12        #[cfg(not(loom))]
13        #[inline(always)]
14        pub const fn new(data: T) -> UnsafeCell<T> {
15            UnsafeCell(Inner::new(data))
16        }
17
18        #[cfg(loom)]
19        #[cfg_attr(loom_nightly, track_caller)]
20        pub fn new(data: T) -> UnsafeCell<T> {
21            UnsafeCell(Inner::new(data))
22        }
23
24        #[inline(always)]
25        #[cfg_attr(loom_nightly, track_caller)]
26        pub fn with<R>(&self, f: impl FnOnce(*const T) -> R) -> R {
27            #[cfg(not(loom))] { f(self.0.get()) }
28            #[cfg(loom)] { self.0.with(f) }
29        }
30
31        #[inline(always)]
32        #[cfg_attr(loom_nightly, track_caller)]
33        pub fn with_mut<R>(&self, f: impl FnOnce(*mut T) -> R) -> R {
34            #[cfg(not(loom))] { f(self.0.get()) }
35            #[cfg(loom)] { self.0.with_mut(f) }
36        }
37
38        #[inline(always)]
39        #[cfg_attr(loom_nightly, track_caller)]
40        pub fn get_mut(&mut self) -> &mut T {
41            // SAFETY: This is the fully safe `UnsafeCell::get_mut()` introduced
42            // in Rust 1.50.0. We don't use it to keep the MSRV down.
43            #[cfg(not(loom))] unsafe { &mut *self.0.get() }
44            #[cfg(loom)] { self.with_mut(|ptr| unsafe { &mut *ptr }) }
45        }
46
47        #[inline(always)]
48        #[cfg_attr(loom_nightly, track_caller)]
49        pub fn into_inner(self) -> T {
50            #[cfg(not(loom))] { self.0.into_inner() }
51            #[cfg(loom)] {
52                let value = self.with(|ptr| unsafe { std::ptr::read(ptr) });
53                std::mem::forget(self);
54                value
55            }
56        }
57    }
58}
59
60#[cfg(loom)] pub use loom::thread_local;
61#[cfg(not(loom))] pub use std::thread_local;
62
63#[cfg(loom)] pub use loom::thread;
64#[cfg(not(loom))] pub use std::thread;