|
1 | 1 | //! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`.
|
2 | 2 |
|
3 | 3 | #[cfg(loom)]
|
4 |
| -pub use loom::cell::UnsafeCell; |
| 4 | +use loom::cell::UnsafeCell as InnerUnsafeCell; |
5 | 5 |
|
6 |
| -#[cfg(not(loom))] |
7 |
| -pub use core::UnsafeCell; |
| 6 | +#[cfg(loom)] |
| 7 | +pub use loom::cell::MutPtr; |
8 | 8 |
|
9 | 9 | #[cfg(not(loom))]
|
10 |
| -mod core { |
11 |
| - /// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with |
12 |
| - /// loom's UnsafeCell. |
13 |
| - #[derive(Debug)] |
14 |
| - pub struct UnsafeCell<T>(core::cell::UnsafeCell<T>); |
15 |
| - |
16 |
| - impl<T> UnsafeCell<T> { |
17 |
| - /// Create a new `UnsafeCell`. |
18 |
| - pub const fn new(data: T) -> UnsafeCell<T> { |
19 |
| - UnsafeCell(core::cell::UnsafeCell::new(data)) |
20 |
| - } |
| 10 | +use core::cell::UnsafeCell as InnerUnsafeCell; |
21 | 11 |
|
22 |
| - /// Access the contents of the `UnsafeCell` through a mut pointer. |
23 |
| - pub fn get_mut(&self) -> MutPtr<T> { |
24 |
| - MutPtr(self.0.get()) |
25 |
| - } |
| 12 | +/// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with |
| 13 | +/// loom's UnsafeCell. |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct UnsafeCell<T>(InnerUnsafeCell<T>); |
| 16 | + |
| 17 | +impl<T> UnsafeCell<T> { |
| 18 | + /// Create a new `UnsafeCell`. |
| 19 | + #[cfg(not(loom))] |
| 20 | + pub const fn new(data: T) -> UnsafeCell<T> { |
| 21 | + UnsafeCell(InnerUnsafeCell::new(data)) |
26 | 22 | }
|
27 | 23 |
|
28 |
| - pub struct MutPtr<T>(*mut T); |
| 24 | + #[cfg(loom)] |
| 25 | + pub fn new(data: T) -> UnsafeCell<T> { |
| 26 | + UnsafeCell(InnerUnsafeCell::new(data)) |
| 27 | + } |
| 28 | + |
| 29 | + /// Access the contents of the `UnsafeCell` through a tracked mut pointer. |
| 30 | + 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 | + } |
29 | 37 |
|
30 |
| - impl<T> MutPtr<T> { |
31 |
| - #[allow(clippy::mut_from_ref)] |
32 |
| - /// SAFETY: the caller must guarantee that the contained `*mut T` is not |
33 |
| - /// null, and must uphold the same safety requirements as for |
34 |
| - /// [`core::primitive::pointer::as_mut`] for the contained `*mut T`. |
35 |
| - pub unsafe fn deref(&self) -> &mut T { |
36 |
| - &mut *self.0 |
| 38 | + /// Access the contents of the `UnsafeCell` mutably. |
| 39 | + pub fn as_mut(&mut self) -> &mut T { |
| 40 | + #[cfg(not(loom))] |
| 41 | + return self.0.get_mut(); |
| 42 | + |
| 43 | + #[cfg(loom)] |
| 44 | + { |
| 45 | + // SAFETY: we have exclusive access to `self`. |
| 46 | + let ptr = self.get_mut(); |
| 47 | + let ptr = unsafe { ptr.deref() }; |
| 48 | + |
| 49 | + // SAFETY: we have exclusive access to `self` for the duration of |
| 50 | + // the borrow. |
| 51 | + unsafe { core::mem::transmute(ptr) } |
37 | 52 | }
|
38 | 53 | }
|
39 | 54 | }
|
| 55 | + |
| 56 | +#[cfg(not(loom))] |
| 57 | +pub struct MutPtr<T>(*mut T); |
| 58 | + |
| 59 | +#[cfg(not(loom))] |
| 60 | +impl<T> MutPtr<T> { |
| 61 | + #[allow(clippy::mut_from_ref)] |
| 62 | + /// SAFETY: the caller must guarantee that the contained `*mut T` is not |
| 63 | + /// null, and must uphold the same safety requirements as for |
| 64 | + /// [`core::primitive::pointer::as_mut`] for the contained `*mut T`. |
| 65 | + pub unsafe fn deref(&self) -> &mut T { |
| 66 | + &mut *self.0 |
| 67 | + } |
| 68 | +} |
0 commit comments