use core::mem;
use core::ops::{Deref, DerefMut};
#[cfg(doc)]
use crate::Gc;
#[non_exhaustive]
#[repr(transparent)]
pub struct Write<T: ?Sized> {
#[doc(hidden)]
pub __inner: T,
}
impl<T: ?Sized> Deref for Write<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.__inner
}
}
impl<T: ?Sized> DerefMut for Write<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.__inner
}
}
impl<T: ?Sized> Write<T> {
#[inline(always)]
pub unsafe fn assume(v: &T) -> &Self {
mem::transmute(v)
}
#[inline]
pub fn from_static(v: &T) -> &Self
where
T: 'static,
{
unsafe { mem::transmute(v) }
}
#[inline]
pub fn from_mut(v: &mut T) -> &mut Self {
unsafe { mem::transmute(v) }
}
#[inline(always)]
#[doc(hidden)]
pub unsafe fn __from_ref_and_ptr(v: &T, _: *const T) -> &Self {
mem::transmute(v)
}
#[inline]
pub fn unlock(&self) -> &T::Unlocked
where
T: Unlock,
{
unsafe { self.__inner.unlock_unchecked() }
}
}
pub trait Unlock {
type Unlocked: ?Sized;
unsafe fn unlock_unchecked(&self) -> &Self::Unlocked;
}
#[doc(inline)]
pub use crate::__field as field;
#[macro_export]
#[doc(hidden)]
macro_rules! __field {
($value:expr, $type:path, $field:ident) => {
match $value {
$crate::barrier::Write {
__inner: $type { ref $field, .. },
..
} => unsafe { $crate::barrier::Write::__from_ref_and_ptr($field, $field as *const _) },
}
};
}
#[doc(inline)]
pub use crate::__unlock as unlock;
#[macro_export]
#[doc(hidden)]
macro_rules! __unlock {
($value:expr, $type:path, $field:ident) => {
$crate::barrier::field!($value, $type, $field).unlock()
};
}