#![no_std]
use ::core::{
mem::transmute,
ops::{
Deref,
DerefMut,
},
};
use raw_transmute::raw_transmute;
macro_rules! create_wrapper {
($name:ident: $($trait:path),+) => {
#[doc = concat!("A wrapper that unsafely makes `T` " $(, "[`", stringify!($trait), "`]", )" + "+ " without requiring it for `T`.")]
#[doc = concat!("Transmuting between this type and `T` is valid, but you must uphold the safety requirements for [`", stringify!($name), "::new`].")]
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name<T: ?Sized>(T);
$(
unsafe impl<T: ?Sized> $trait for $name<T> {}
)+
impl<T> $name<T> {
#[doc = concat!("Creates a new [`", stringify!($name), "<T>`] wrapping the given value.")]
#[doc = concat!("Creating [`", stringify!($name), "<T>`] requires unsafe for construction only.")]
#[must_use = "this is a zero-cost wrapper."]
#[inline(always)]
pub const unsafe fn new(value: T) -> Self {
Self(value)
}
#[doc = concat!("A safe alternative to construct [`", stringify!($name), "<T>`] when `T` already implements the required traits.")]
#[must_use = "this is a zero-cost wrapper."]
#[inline(always)]
pub const fn safe_new(value: T) -> Self where $(T: $trait),+ {
unsafe { Self::new(value) }
}
#[doc = concat!("Consumes the [`", stringify!($name), "<T>`] and returns the inner value.")]
#[must_use = "unwrapping is pure. Did you mean to `drop` the wrapper instead?"]
#[inline(always)]
pub const fn into_inner(this: Self) -> T {
unsafe { raw_transmute::<Self, T>(this) }
}
}
impl<T: ?Sized> $name<T> {
#[doc = concat!("Returns a reference to the inner value of the [`", stringify!($name), "<T>`].")]
#[must_use = "dereferencing is pure."]
#[inline(always)]
pub const fn inner_ref(this: &Self) -> &T {
&this.0
}
#[doc = concat!("Returns a mutable reference to the inner value of the [`", stringify!($name), "<T>`].")]
#[must_use = "dereferencing is pure."]
#[inline(always)]
pub const fn inner_mut(this: &mut Self) -> &mut T {
&mut this.0
}
#[doc = concat!("Maps `&T` to [`&", stringify!($name), "<T>`](", stringify!($name), ").")]
#[doc = concat!("See [`", stringify!($name), "<T>::new`].")]
#[must_use = "creating references is pure."]
#[inline(always)]
pub const unsafe fn from_ref(value: &T) -> &Self {
unsafe { transmute::<&T, &Self>(value) }
}
#[doc = concat!("A safe alternative to [`", stringify!($name), "<T>::from_ref`] when `T` already implements the required traits.")]
#[must_use = "creating references is pure."]
#[inline(always)]
pub const fn safe_from_ref(value: &T) -> &Self where $(T: $trait),+ {
unsafe { Self::from_ref(value) }
}
#[doc = concat!("Maps `&mut T` to [`&mut ", stringify!($name), "<T>`](", stringify!($name), ").")]
#[doc = concat!("See [`", stringify!($name), "<T>::new`].")]
#[must_use = "creating references is pure."]
#[inline(always)]
pub const unsafe fn from_mut(value: &mut T) -> &mut Self {
unsafe { transmute::<&mut T, &mut Self>(value) }
}
#[doc = concat!("A safe alternative to [`", stringify!($name), "<T>::from_mut`] when `T` already implements the required traits.")]
#[must_use = "creating references is pure."]
#[inline(always)]
pub const fn safe_from_mut(value: &mut T) -> &mut Self where $(T: $trait),+ {
unsafe { Self::from_mut(value) }
}
}
impl<T: ?Sized> Deref for $name<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
Self::inner_ref(self)
}
}
impl<T: ?Sized> DerefMut for $name<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
Self::inner_mut(self)
}
}
};
}
create_wrapper! { UnsafeSend: Send }
create_wrapper! { UnsafeSync: Sync }
create_wrapper! { UnsafeSendSync: Send, Sync }