1//! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`.
23#[cfg(loom)]
4use loom::cell::UnsafeCell as InnerUnsafeCell;
56#[cfg(loom)]
7pub use loom::cell::MutPtr;
89#[cfg(not(loom))]
10use core::cell::UnsafeCell as InnerUnsafeCell;
1112/// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with
13/// loom's UnsafeCell.
14#[derive(Debug)]
15pub struct UnsafeCell<T>(InnerUnsafeCell<T>);
1617impl<T> UnsafeCell<T> {
18/// Create a new `UnsafeCell`.
19#[cfg(not(loom))]
20pub const fn new(data: T) -> UnsafeCell<T> {
21 UnsafeCell(InnerUnsafeCell::new(data))
22 }
2324#[cfg(loom)]
25pub fn new(data: T) -> UnsafeCell<T> {
26 UnsafeCell(InnerUnsafeCell::new(data))
27 }
2829/// Access the contents of the `UnsafeCell` through a tracked mut pointer.
30pub fn get_mut(&self) -> MutPtr<T> {
31#[cfg(loom)]
32return self.0.get_mut();
3334#[cfg(not(loom))]
35return MutPtr(self.0.get());
36 }
3738/// Access the contents of the `UnsafeCell` mutably.
39pub fn as_mut(&mut self) -> &mut T {
40#[cfg(not(loom))]
41return self.0.get_mut();
4243#[cfg(loom)]
44{
45// SAFETY: we have exclusive access to `self`.
46let ptr = self.get_mut();
47let ptr = unsafe { ptr.deref() };
4849// SAFETY: we have exclusive access to `self` for the duration of
50 // the borrow.
51unsafe { core::mem::transmute(ptr) }
52 }
53 }
54}
5556#[cfg(not(loom))]
57pub struct MutPtr<T>(*mut T);
5859#[cfg(not(loom))]
60impl<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`.
65pub unsafe fn deref(&self) -> &mut T {
66&mut *self.0
67}
68}