aboutsummaryrefslogtreecommitdiff
path: root/rtic-sync/src/unsafecell.rs
blob: e3730d48ad8c3e10ab369acedc04a9b93842bbf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Compat layer for [`core::cell::UnsafeCell`] and `loom::cell::UnsafeCell`.

#[cfg(loom)]
pub use loom::cell::UnsafeCell;

#[cfg(not(loom))]
pub use core::UnsafeCell;

#[cfg(not(loom))]
mod core {
    /// An [`core::cell::UnsafeCell`] wrapper that provides compatibility with
    /// loom's UnsafeCell.
    #[derive(Debug)]
    pub struct UnsafeCell<T>(core::cell::UnsafeCell<T>);

    impl<T> UnsafeCell<T> {
        /// Create a new `UnsafeCell`.
        pub const fn new(data: T) -> UnsafeCell<T> {
            UnsafeCell(core::cell::UnsafeCell::new(data))
        }

        /// Access the contents of the `UnsafeCell` through a mut pointer.
        pub fn get_mut(&self) -> MutPtr<T> {
            MutPtr(self.0.get())
        }
    }

    pub struct MutPtr<T>(*mut T);

    impl<T> MutPtr<T> {
        #[allow(clippy::mut_from_ref)]
        /// SAFETY: the caller must guarantee that the contained `*mut T` is not
        /// null, and must uphold the same safety requirements as for
        /// [`core::primitive::pointer::as_mut`] for the contained `*mut T`.
        pub unsafe fn deref(&self) -> &mut T {
            &mut *self.0
        }
    }
}