1#[cfg(loom)]
4use loom::cell::UnsafeCell as InnerUnsafeCell;
5
6#[cfg(loom)]
7pub use loom::cell::MutPtr;
8
9#[cfg(not(loom))]
10use core::cell::UnsafeCell as InnerUnsafeCell;
11
12#[derive(Debug)]
15pub struct UnsafeCell<T>(InnerUnsafeCell<T>);
16
17impl<T> UnsafeCell<T> {
18 #[cfg(not(loom))]
20 pub const fn new(data: T) -> UnsafeCell<T> {
21 UnsafeCell(InnerUnsafeCell::new(data))
22 }
23
24 #[cfg(loom)]
25 pub fn new(data: T) -> UnsafeCell<T> {
26 UnsafeCell(InnerUnsafeCell::new(data))
27 }
28
29 pub fn get_mut(&self) -> MutPtr<T> {
31 #[cfg(loom)]
32 return self.0.get_mut();
33
34 #[cfg(not(loom))]
35 return MutPtr(self.0.get());
36 }
37
38 pub fn as_mut(&mut self) -> &mut T {
40 #[cfg(not(loom))]
41 return self.0.get_mut();
42
43 #[cfg(loom)]
44 {
45 let ptr = self.get_mut();
47 let ptr = unsafe { ptr.deref() };
48
49 unsafe { core::mem::transmute(ptr) }
52 }
53 }
54}
55
56#[cfg(not(loom))]
57pub struct MutPtr<T>(*mut T);
58
59#[cfg(not(loom))]
60impl<T> MutPtr<T> {
61 #[allow(clippy::mut_from_ref)]
62 pub unsafe fn deref(&self) -> &mut T {
66 &mut *self.0
67 }
68}