1use core::error::Error;
6use core::fmt::{self, Debug, Display, Formatter};
7#[cfg(not(no_global_oom_handling))]
8use core::intrinsics::const_allocate;
9use core::marker::PhantomData;
10#[cfg(not(no_global_oom_handling))]
11use core::marker::Unsize;
12#[cfg(not(no_global_oom_handling))]
13use core::mem::{self, SizedTypeProperties};
14use core::ops::{Deref, DerefMut};
15use core::ptr::{self, NonNull, Pointee};
16
17use crate::alloc::{self, Layout, LayoutError};
18
19#[unstable(feature = "thin_box", issue = "92791")]
37pub struct ThinBox<T: ?Sized> {
38 ptr: WithOpaqueHeader,
41 _marker: PhantomData<T>,
42}
43
44#[unstable(feature = "thin_box", issue = "92791")]
46unsafe impl<T: ?Sized + Send> Send for ThinBox<T> {}
47
48#[unstable(feature = "thin_box", issue = "92791")]
50unsafe impl<T: ?Sized + Sync> Sync for ThinBox<T> {}
51
52#[unstable(feature = "thin_box", issue = "92791")]
53impl<T> ThinBox<T> {
54 #[cfg(not(no_global_oom_handling))]
68 pub fn new(value: T) -> Self {
69 let meta = ptr::metadata(&value);
70 let ptr = WithOpaqueHeader::new(meta, value);
71 ThinBox { ptr, _marker: PhantomData }
72 }
73
74 pub fn try_new(value: T) -> Result<Self, core::alloc::AllocError> {
90 let meta = ptr::metadata(&value);
91 WithOpaqueHeader::try_new(meta, value).map(|ptr| ThinBox { ptr, _marker: PhantomData })
92 }
93}
94
95#[unstable(feature = "thin_box", issue = "92791")]
96impl<Dyn: ?Sized> ThinBox<Dyn> {
97 #[cfg(not(no_global_oom_handling))]
111 pub fn new_unsize<T>(value: T) -> Self
112 where
113 T: Unsize<Dyn>,
114 {
115 if size_of::<T>() == 0 {
116 let ptr = WithOpaqueHeader::new_unsize_zst::<Dyn, T>(value);
117 ThinBox { ptr, _marker: PhantomData }
118 } else {
119 let meta = ptr::metadata(&value as &Dyn);
120 let ptr = WithOpaqueHeader::new(meta, value);
121 ThinBox { ptr, _marker: PhantomData }
122 }
123 }
124}
125
126#[unstable(feature = "thin_box", issue = "92791")]
127impl<T: ?Sized + Debug> Debug for ThinBox<T> {
128 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
129 Debug::fmt(self.deref(), f)
130 }
131}
132
133#[unstable(feature = "thin_box", issue = "92791")]
134impl<T: ?Sized + Display> Display for ThinBox<T> {
135 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
136 Display::fmt(self.deref(), f)
137 }
138}
139
140#[unstable(feature = "thin_box", issue = "92791")]
141impl<T: ?Sized> Deref for ThinBox<T> {
142 type Target = T;
143
144 fn deref(&self) -> &T {
145 let value = self.data();
146 let metadata = self.meta();
147 let pointer = ptr::from_raw_parts(value as *const (), metadata);
148 unsafe { &*pointer }
149 }
150}
151
152#[unstable(feature = "thin_box", issue = "92791")]
153impl<T: ?Sized> DerefMut for ThinBox<T> {
154 fn deref_mut(&mut self) -> &mut T {
155 let value = self.data();
156 let metadata = self.meta();
157 let pointer = ptr::from_raw_parts_mut::<T>(value as *mut (), metadata);
158 unsafe { &mut *pointer }
159 }
160}
161
162#[unstable(feature = "thin_box", issue = "92791")]
163impl<T: ?Sized> Drop for ThinBox<T> {
164 fn drop(&mut self) {
165 unsafe {
166 let value = self.deref_mut();
167 let value = value as *mut T;
168 self.with_header().drop::<T>(value);
169 }
170 }
171}
172
173#[unstable(feature = "thin_box", issue = "92791")]
174impl<T: ?Sized> ThinBox<T> {
175 fn meta(&self) -> <T as Pointee>::Metadata {
176 unsafe { *self.with_header().header() }
179 }
180
181 fn data(&self) -> *mut u8 {
182 self.with_header().value()
183 }
184
185 fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
186 unsafe { &*((&raw const self.ptr) as *const WithHeader<_>) }
188 }
189}
190
191#[repr(transparent)]
197struct WithHeader<H>(NonNull<u8>, PhantomData<H>);
198
199#[repr(transparent)]
202struct WithOpaqueHeader(NonNull<u8>);
203
204impl WithOpaqueHeader {
205 #[cfg(not(no_global_oom_handling))]
206 fn new<H, T>(header: H, value: T) -> Self {
207 let ptr = WithHeader::new(header, value);
208 Self(ptr.0)
209 }
210
211 #[cfg(not(no_global_oom_handling))]
212 fn new_unsize_zst<Dyn, T>(value: T) -> Self
213 where
214 Dyn: ?Sized,
215 T: Unsize<Dyn>,
216 {
217 let ptr = WithHeader::<<Dyn as Pointee>::Metadata>::new_unsize_zst::<Dyn, T>(value);
218 Self(ptr.0)
219 }
220
221 fn try_new<H, T>(header: H, value: T) -> Result<Self, core::alloc::AllocError> {
222 WithHeader::try_new(header, value).map(|ptr| Self(ptr.0))
223 }
224}
225
226impl<H> WithHeader<H> {
227 #[cfg(not(no_global_oom_handling))]
228 fn new<T>(header: H, value: T) -> WithHeader<H> {
229 let value_layout = Layout::new::<T>();
230 let Ok((layout, value_offset)) = Self::alloc_layout(value_layout) else {
231 alloc::handle_alloc_error(Layout::new::<()>());
238 };
239
240 unsafe {
241 let ptr = if layout.size() == 0 {
245 debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
248 layout.dangling()
249 } else {
250 let ptr = alloc::alloc(layout);
251 if ptr.is_null() {
252 alloc::handle_alloc_error(layout);
253 }
254 let ptr = ptr.add(value_offset) as *mut _;
257
258 NonNull::new_unchecked(ptr)
259 };
260
261 let result = WithHeader(ptr, PhantomData);
262 ptr::write(result.header(), header);
263 ptr::write(result.value().cast(), value);
264
265 result
266 }
267 }
268
269 fn try_new<T>(header: H, value: T) -> Result<WithHeader<H>, core::alloc::AllocError> {
272 let value_layout = Layout::new::<T>();
273 let Ok((layout, value_offset)) = Self::alloc_layout(value_layout) else {
274 return Err(core::alloc::AllocError);
275 };
276
277 unsafe {
278 let ptr = if layout.size() == 0 {
282 debug_assert!(value_offset == 0 && size_of::<T>() == 0 && size_of::<H>() == 0);
285 layout.dangling()
286 } else {
287 let ptr = alloc::alloc(layout);
288 if ptr.is_null() {
289 return Err(core::alloc::AllocError);
290 }
291
292 let ptr = ptr.add(value_offset) as *mut _;
295
296 NonNull::new_unchecked(ptr)
297 };
298
299 let result = WithHeader(ptr, PhantomData);
300 ptr::write(result.header(), header);
301 ptr::write(result.value().cast(), value);
302
303 Ok(result)
304 }
305 }
306
307 #[cfg(not(no_global_oom_handling))]
309 fn new_unsize_zst<Dyn, T>(value: T) -> WithHeader<H>
310 where
311 Dyn: Pointee<Metadata = H> + ?Sized,
312 T: Unsize<Dyn>,
313 {
314 assert!(size_of::<T>() == 0);
315
316 const fn max(a: usize, b: usize) -> usize {
317 if a > b { a } else { b }
318 }
319
320 let alloc: &<Dyn as Pointee>::Metadata = const {
325 let alloc_align = max(align_of::<T>(), align_of::<<Dyn as Pointee>::Metadata>());
329
330 let alloc_size = max(align_of::<T>(), size_of::<<Dyn as Pointee>::Metadata>());
331
332 unsafe {
333 let alloc: *mut u8 = const_allocate(alloc_size, alloc_align);
335
336 let metadata_offset =
337 alloc_size.checked_sub(size_of::<<Dyn as Pointee>::Metadata>()).unwrap();
338 let metadata_ptr: *mut <Dyn as Pointee>::Metadata =
340 alloc.add(metadata_offset).cast();
341 metadata_ptr.write(ptr::metadata::<Dyn>(ptr::dangling::<T>() as *const Dyn));
343
344 &*(metadata_ptr)
346 }
347 };
348
349 let value_ptr =
351 unsafe { (alloc as *const <Dyn as Pointee>::Metadata).add(1) }.cast::<T>().cast_mut();
352 debug_assert!(value_ptr.is_aligned());
353 mem::forget(value);
354 WithHeader(NonNull::new(value_ptr.cast()).unwrap(), PhantomData)
355 }
356
357 unsafe fn drop<T: ?Sized>(&self, value: *mut T) {
361 struct DropGuard<H> {
362 ptr: NonNull<u8>,
363 value_layout: Layout,
364 _marker: PhantomData<H>,
365 }
366
367 impl<H> Drop for DropGuard<H> {
368 fn drop(&mut self) {
369 if self.value_layout.size() == 0 {
371 return;
372 }
373
374 unsafe {
375 let (layout, value_offset) =
377 WithHeader::<H>::alloc_layout(self.value_layout).unwrap_unchecked();
378
379 debug_assert!(layout.size() != 0);
381 alloc::dealloc(self.ptr.as_ptr().sub(value_offset), layout);
382 }
383 }
384 }
385
386 unsafe {
387 let _guard = DropGuard {
389 ptr: self.0,
390 value_layout: Layout::for_value_raw(value),
391 _marker: PhantomData::<H>,
392 };
393
394 ptr::drop_in_place::<T>(value);
397 }
398 }
399
400 fn header(&self) -> *mut H {
401 let hp = unsafe { self.0.as_ptr().sub(Self::header_size()) as *mut H };
409 debug_assert!(hp.is_aligned());
410 hp
411 }
412
413 fn value(&self) -> *mut u8 {
414 self.0.as_ptr()
415 }
416
417 const fn header_size() -> usize {
418 size_of::<H>()
419 }
420
421 fn alloc_layout(value_layout: Layout) -> Result<(Layout, usize), LayoutError> {
422 Layout::new::<H>().extend(value_layout)
423 }
424}
425
426#[unstable(feature = "thin_box", issue = "92791")]
427impl<T: ?Sized + Error> Error for ThinBox<T> {
428 fn source(&self) -> Option<&(dyn Error + 'static)> {
429 self.deref().source()
430 }
431}