//! Copy pasted from std::cell::SyncUnsafeCell1//! can be removed once the feature stabilizes.2use std::cell::UnsafeCell;34/// [`UnsafeCell`], but [`Sync`].5///6/// This is just an [`UnsafeCell`], except it implements [`Sync`]7/// if `T` implements [`Sync`].8///9/// [`UnsafeCell`] doesn't implement [`Sync`], to prevent accidental misuse.10/// You can use [`SyncUnsafeCell`] instead of [`UnsafeCell`] to allow it to be11/// shared between threads, if that's intentional.12/// Providing proper synchronization is still the task of the user,13/// making this type just as unsafe to use.14///15/// See [`UnsafeCell`] for details.16#[repr(transparent)]17pub struct SyncUnsafeCell<T: ?Sized> {18value: UnsafeCell<T>,19}2021unsafe impl<T: ?Sized + Sync> Sync for SyncUnsafeCell<T> {}2223impl<T> SyncUnsafeCell<T> {24/// Constructs a new instance of [`SyncUnsafeCell`] which will wrap the specified value.25#[inline]26pub fn new(value: T) -> Self {27Self {28value: UnsafeCell::new(value),29}30}3132/// Unwraps the value.33#[inline]34pub fn into_inner(self) -> T {35self.value.into_inner()36}37}3839impl<T: ?Sized> SyncUnsafeCell<T> {40/// Gets a mutable pointer to the wrapped value.41///42/// This can be cast to a pointer of any kind.43/// Ensure that the access is unique (no active references, mutable or not)44/// when casting to `&mut T`, and ensure that there are no mutations45/// or mutable aliases going on when casting to `&T`46#[inline]47pub fn get(&self) -> *mut T {48self.value.get()49}5051/// Returns a mutable reference to the underlying data.52///53/// This call borrows the [`SyncUnsafeCell`] mutably (at compile-time) which54/// guarantees that we possess the only reference.55#[inline]56pub fn get_mut(&mut self) -> &mut T {57self.value.get_mut()58}5960/// Gets a mutable pointer to the wrapped value.61///62/// See [`UnsafeCell::get`] for details.63#[inline]64pub fn raw_get(this: *const Self) -> *mut T {65// We can just cast the pointer from `SyncUnsafeCell<T>` to `T` because66// of #[repr(transparent)] on both SyncUnsafeCell and UnsafeCell.67// See UnsafeCell::raw_get.68this as *const T as *mut T69}70}7172impl<T: Default> Default for SyncUnsafeCell<T> {73/// Creates an `SyncUnsafeCell`, with the `Default` value for T.74fn default() -> SyncUnsafeCell<T> {75SyncUnsafeCell::new(Default::default())76}77}7879impl<T> From<T> for SyncUnsafeCell<T> {80/// Creates a new [`SyncUnsafeCell<T>`] containing the given value.81fn from(t: T) -> SyncUnsafeCell<T> {82SyncUnsafeCell::new(t)83}84}858687