1#![no_std]
33
34use ::core::{
35 mem::transmute,
36 ops::{
37 Deref,
38 DerefMut,
39 },
40};
41use raw_transmute::raw_transmute;
42
43macro_rules! create_wrapper {
44 ($name:ident: $($trait:path),+) => {
45 #[doc = concat!("A wrapper that unsafely makes `T` " $(, "[`", stringify!($trait), "`]", )" + "+ " without requiring it for `T`.")]
46 #[doc = concat!("Transmuting between this type and `T` is valid, but you must uphold the safety requirements for [`", stringify!($name), "::new`].")]
51 #[repr(transparent)]
52 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
53 pub struct $name<T: ?Sized>(T);
54
55 $(
56 unsafe impl<T: ?Sized> $trait for $name<T> {}
59 )+
60
61 impl<T> $name<T> {
62 #[doc = concat!("Creates a new [`", stringify!($name), "<T>`] wrapping the given value.")]
63 #[doc = concat!("Creating [`", stringify!($name), "<T>`] requires unsafe for construction only.")]
67 #[must_use = "this is a zero-cost wrapper."]
70 #[inline(always)]
71 pub const unsafe fn new(value: T) -> Self {
72 Self(value)
73 }
74
75 #[doc = concat!("A safe alternative to construct [`", stringify!($name), "<T>`] when `T` already implements the required traits.")]
76 #[must_use = "this is a zero-cost wrapper."]
77 #[inline(always)]
78 pub const fn safe_new(value: T) -> Self where $(T: $trait),+ {
79 unsafe { Self::new(value) }
81 }
82
83 #[doc = concat!("Consumes the [`", stringify!($name), "<T>`] and returns the inner value.")]
84 #[must_use = "unwrapping is pure. Did you mean to `drop` the wrapper instead?"]
85 #[inline(always)]
86 pub const fn into_inner(this: Self) -> T {
87 unsafe { raw_transmute::<Self, T>(this) }
89 }
90 }
91
92 impl<T: ?Sized> $name<T> {
93 #[doc = concat!("Returns a reference to the inner value of the [`", stringify!($name), "<T>`].")]
94 #[must_use = "dereferencing is pure."]
97 #[inline(always)]
98 pub const fn inner_ref(this: &Self) -> &T {
99 &this.0
100 }
101
102 #[doc = concat!("Returns a mutable reference to the inner value of the [`", stringify!($name), "<T>`].")]
103 #[must_use = "dereferencing is pure."]
106 #[inline(always)]
107 pub const fn inner_mut(this: &mut Self) -> &mut T {
108 &mut this.0
109 }
110
111 #[doc = concat!("Maps `&T` to [`&", stringify!($name), "<T>`](", stringify!($name), ").")]
112 #[doc = concat!("See [`", stringify!($name), "<T>::new`].")]
118 #[must_use = "creating references is pure."]
119 #[inline(always)]
120 pub const unsafe fn from_ref(value: &T) -> &Self {
121 unsafe { transmute::<&T, &Self>(value) }
123 }
124
125 #[doc = concat!("A safe alternative to [`", stringify!($name), "<T>::from_ref`] when `T` already implements the required traits.")]
126 #[must_use = "creating references is pure."]
127 #[inline(always)]
128 pub const fn safe_from_ref(value: &T) -> &Self where $(T: $trait),+ {
129 unsafe { Self::from_ref(value) }
131 }
132
133 #[doc = concat!("Maps `&mut T` to [`&mut ", stringify!($name), "<T>`](", stringify!($name), ").")]
134 #[doc = concat!("See [`", stringify!($name), "<T>::new`].")]
140 #[must_use = "creating references is pure."]
141 #[inline(always)]
142 pub const unsafe fn from_mut(value: &mut T) -> &mut Self {
143 unsafe { transmute::<&mut T, &mut Self>(value) }
145 }
146
147 #[doc = concat!("A safe alternative to [`", stringify!($name), "<T>::from_mut`] when `T` already implements the required traits.")]
148 #[must_use = "creating references is pure."]
149 #[inline(always)]
150 pub const fn safe_from_mut(value: &mut T) -> &mut Self where $(T: $trait),+ {
151 unsafe { Self::from_mut(value) }
153 }
154 }
155
156 impl<T: ?Sized> Deref for $name<T> {
157 type Target = T;
158
159 #[inline(always)]
160 fn deref(&self) -> &Self::Target {
161 Self::inner_ref(self)
162 }
163 }
164
165 impl<T: ?Sized> DerefMut for $name<T> {
166 #[inline(always)]
167 fn deref_mut(&mut self) -> &mut Self::Target {
168 Self::inner_mut(self)
169 }
170 }
171 };
172}
173
174create_wrapper! { UnsafeSend: Send }
175create_wrapper! { UnsafeSync: Sync }
176create_wrapper! { UnsafeSendSync: Send, Sync }