|
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 | | - } |
21 | | - |
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 | | - } |
| 10 | +use core::cell::UnsafeCell as InnerUnsafeCell; |
| 11 | + |
| 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)) |
| 22 | + } |
| 23 | + |
| 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 | + #[cfg(not(loom))] |
| 31 | + pub fn get_mut(&self) -> MutPtr<T> { |
| 32 | + MutPtr(self.0.get()) |
| 33 | + } |
| 34 | + |
| 35 | + /// Access the contents of the `UnsafeCell` through a tracked mut pointer. |
| 36 | + #[cfg(loom)] |
| 37 | + pub fn get_mut(&self) -> MutPtr<T> { |
| 38 | + self.0.get_mut() |
| 39 | + } |
| 40 | + |
| 41 | + /// Access the contents of the `UnsafeCell` mutably. |
| 42 | + #[cfg(not(loom))] |
| 43 | + pub fn as_mut(&mut self) -> &mut T { |
| 44 | + self.0.get_mut() |
26 | 45 | } |
27 | 46 |
|
28 | | - pub struct MutPtr<T>(*mut T); |
| 47 | + /// Access the contents of the `UnsafeCell` mutably. |
| 48 | + #[cfg(loom)] |
| 49 | + pub fn as_mut<'a>(&'a mut self) -> &'a mut T { |
| 50 | + // SAFETY: we have exclusive access to `self`. |
| 51 | + let ptr = self.get_mut(); |
| 52 | + let ptr = unsafe { ptr.deref() }; |
29 | 53 |
|
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 |
37 | | - } |
| 54 | + // SAFETY: we have exclusive access to `self` for the duration of |
| 55 | + // the borrow. |
| 56 | + unsafe { core::mem::transmute(ptr) } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(not(loom))] |
| 61 | +pub struct MutPtr<T>(*mut T); |
| 62 | + |
| 63 | +#[cfg(not(loom))] |
| 64 | +impl<T> MutPtr<T> { |
| 65 | + #[allow(clippy::mut_from_ref)] |
| 66 | + /// SAFETY: the caller must guarantee that the contained `*mut T` is not |
| 67 | + /// null, and must uphold the same safety requirements as for |
| 68 | + /// [`core::primitive::pointer::as_mut`] for the contained `*mut T`. |
| 69 | + pub unsafe fn deref(&self) -> &mut T { |
| 70 | + &mut *self.0 |
38 | 71 | } |
39 | 72 | } |
0 commit comments