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