/// See [`Exclusive`](https://github.com/rust-lang/rust/issues/98407) for stdlib's upcoming implementation, /// which should replace this one entirely. /// /// Provides a wrapper that allows making any type unconditionally [`Sync`] by only providing mutable access. #[repr(transparent)] pub struct SyncCell { inner: T, } impl SyncCell { /// Construct a new instance of a `SyncCell` from the given value. pub fn new(inner: T) -> Self { Self { inner } } /// Deconstruct this `SyncCell` into its inner value. pub fn to_inner(Self { inner }: Self) -> T { inner } } impl SyncCell { /// Get a reference to this `SyncCell`'s inner value. pub fn get(&mut self) -> &mut T { &mut self.inner } /// Build a mutable reference to a `SyncCell` from a mutable reference /// to its inner value, to skip constructing with [`new()`](SyncCell::new()). pub fn from_mut(r: &'_ mut T) -> &'_ mut SyncCell { // SAFETY: repr is transparent, so refs have the same layout; and `SyncCell` properties are `&mut`-agnostic unsafe { &mut *(r as *mut T as *mut SyncCell) } } } // SAFETY: `Sync` only allows multithreaded access via immutable reference. // As `SyncCell` requires an exclusive reference to access the wrapped value, // marking this type as `Sync` does not actually allow threaded access to the inner value. unsafe impl Sync for SyncCell {}