1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![no_std]
extern crate alloc;
extern crate self as thin;
use {crate::polyfill::*, core::ptr};
mod polyfill;
use priv_in_pub::Erased;
#[doc(hidden)]
pub mod priv_in_pub {
pub struct Erased {
#[allow(unused)]
raw: (),
}
}
#[macro_export]
macro_rules! make_fat {
() => {
fn make_fat(ptr: *mut [$crate::priv_in_pub::Erased]) -> *mut Self {
ptr as *mut Self
}
};
}
pub unsafe trait Thinnable {
type Head: Sized;
type SliceItem: Sized;
fn get_length(head: &Self::Head) -> usize;
fn make_fat(ptr: *mut [Erased]) -> *mut Self;
fn make_thin(fat: ptr::NonNull<Self>) -> ptr::NonNull<Erased> {
fat.cast()
}
unsafe fn make_fat_const(thin: ptr::NonNull<Erased>) -> ptr::NonNull<Self> {
let len = Self::get_length(thin.cast().as_ref());
ptr::NonNull::new_unchecked(Self::make_fat(make_slice(thin.as_ptr(), len) as *mut _))
}
unsafe fn make_fat_mut(thin: ptr::NonNull<Erased>) -> ptr::NonNull<Self> {
let len = Self::get_length(thin.cast().as_ref());
ptr::NonNull::new_unchecked(Self::make_fat(make_slice_mut(thin.as_ptr(), len)))
}
}
macro_rules! thin_holder {
( for $thin:ident<T> as $fat:ident<T> with $make_fat:ident ) => {
impl<T: ?Sized + Thinnable> Drop for $thin<T> {
fn drop(&mut self) {
unsafe { drop::<$fat<T>>($fat::from_raw(T::$make_fat(self.raw).as_ptr())) }
}
}
impl<T: ?Sized + Thinnable> $thin<T> {
pub unsafe fn from_thin(thin: ptr::NonNull<Erased>) -> Self {
Self {
raw: thin,
marker: PhantomData,
}
}
pub fn into_thin(this: Self) -> ptr::NonNull<Erased> {
let this = ManuallyDrop::new(this);
this.raw
}
pub unsafe fn from_fat(fat: ptr::NonNull<T>) -> Self {
Self::from_thin(T::make_thin(fat))
}
pub fn into_fat(this: Self) -> $fat<T> {
unsafe {
let this = ManuallyDrop::new(this);
$fat::from_raw(T::$make_fat(this.raw).as_ptr())
}
}
}
impl<T: ?Sized + Thinnable> From<$fat<T>> for $thin<T> {
fn from(this: $fat<T>) -> $thin<T> {
unsafe {
$thin::from_fat(ptr::NonNull::new_unchecked($fat::into_raw(this) as *mut _))
}
}
}
impl<T: ?Sized + Thinnable> Clone for $thin<T>
where
$fat<T>: Clone,
{
fn clone(&self) -> Self {
unsafe {
let this =
ManuallyDrop::new($fat::from_raw(T::make_fat_const(self.raw).as_ptr()));
ManuallyDrop::into_inner(ManuallyDrop::clone(&this)).into()
}
}
}
impl<T: ?Sized + Thinnable> Deref for $thin<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*T::make_fat_const(self.raw).as_ptr() }
}
}
};
}
macro_rules! std_traits {
(for $ty:ident $(<$T:ident>)? as $raw:ty $(where $($tt:tt)*)?) => {
impl $(<$T>)? core::fmt::Debug for $ty $(<$T>)? where $raw: core::fmt::Debug, $($($tt)*)? {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
<$raw as core::fmt::Debug>::fmt(self, f)
}
}
impl $(<$T>)? core::fmt::Display for $ty $(<$T>)? where $raw: core::fmt::Display, $($($tt)*)? {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
<$raw as core::fmt::Display>::fmt(self, f)
}
}
impl<$($T,)? O: ?Sized> cmp::PartialOrd<O> for $ty $(<$T>)? where $raw: cmp::PartialOrd<O>, $($($tt)*)? {
fn partial_cmp(&self, other: &O) -> Option<cmp::Ordering> {
<$raw as cmp::PartialOrd<O>>::partial_cmp(self, other)
}
}
impl<$($T,)? O: ?Sized> cmp::PartialEq<O> for $ty $(<$T>)? where $raw: cmp::PartialEq<O>, $($($tt)*)? {
fn eq(&self, other: &O) -> bool {
<$raw as cmp::PartialEq<O>>::eq(self, other)
}
}
};
}
macro_rules! total_std_traits {
(for $ty:ident $(<$T:ident>)? as $raw:ty $(where $($tt:tt)*)?) => {
std_traits!(for $ty $(<$T>)? as $raw $(where $($tt:tt)*)?);
impl $(<$T>)? cmp::Eq for $ty $(<$T>)? where $raw: cmp::Eq, $($($tt)*)? {}
impl $(<$T>)? cmp::Ord for $ty $(<$T>)? where $raw: cmp::Ord, $($($tt)*)? {
fn cmp(&self, other: &Self) -> cmp::Ordering {
<$raw as cmp::Ord>::cmp(self, other)
}
}
impl $(<$T>)? cmp::PartialEq for $ty $(<$T>)? where $raw: cmp::PartialEq, $($($tt)*)? {
fn eq(&self, other: &Self) -> bool {
<$raw as cmp::PartialEq>::eq(self, other)
}
}
impl $(<$T>)? cmp::PartialOrd for $ty $(<$T>)? where $raw: cmp::PartialOrd, $($($tt)*)? {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
<$raw as cmp::PartialOrd>::partial_cmp(self, other)
}
}
};
}
mod slice;
pub use slice::ThinSlice as Slice;
mod boxed;
pub use boxed::ThinBox as Box;
mod rc;
pub use rc::ThinRc as Rc;
mod arc;
pub use arc::ThinArc as Arc;