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;
12use core::mem;
13#[cfg(not(no_global_oom_handling))]
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")]
39pub struct ThinBox<T: ?Sized> {
40 ptr: WithOpaqueHeader,
43 _marker: PhantomData<T>,
44}
45
46#[unstable(feature = "thin_box", issue = "92791")]
48unsafe impl<T: ?Sized + Send> Send for ThinBox<T> {}
49
50#[unstable(feature = "thin_box", issue = "92791")]
52unsafe impl<T: ?Sized + Sync> Sync for ThinBox<T> {}
53
54#[unstable(feature = "thin_box", issue = "92791")]
55impl<T> ThinBox<T> {
56 #[cfg(not(no_global_oom_handling))]
70 pub fn new(value: T) -> Self {
71 let meta = ptr::metadata(&value);
72 let ptr = WithOpaqueHeader::new(meta, value);
73 ThinBox { ptr, _marker: PhantomData }
74 }
75
76 pub fn try_new(value: T) -> Result<Self, core::alloc::AllocError> {
92 let meta = ptr::metadata(&value);
93 WithOpaqueHeader::try_new(meta, value).map(|ptr| ThinBox { ptr, _marker: PhantomData })
94 }
95}
96
97#[unstable(feature = "thin_box", issue = "92791")]
98impl<Dyn: ?Sized> ThinBox<Dyn> {
99 #[cfg(not(no_global_oom_handling))]
113 pub fn new_unsize<T>(value: T) -> Self
114 where
115 T: Unsize<Dyn>,
116 {
117 if mem::size_of::<T>() == 0 {
118 let ptr = WithOpaqueHeader::new_unsize_zst::<Dyn, T>(value);
119 ThinBox { ptr, _marker: PhantomData }
120 } else {
121 let meta = ptr::metadata(&value as &Dyn);
122 let ptr = WithOpaqueHeader::new(meta, value);
123 ThinBox { ptr, _marker: PhantomData }
124 }
125 }
126}
127
128#[unstable(feature = "thin_box", issue = "92791")]
129impl<T: ?Sized + Debug> Debug for ThinBox<T> {
130 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
131 Debug::fmt(self.deref(), f)
132 }
133}
134
135#[unstable(feature = "thin_box", issue = "92791")]
136impl<T: ?Sized + Display> Display for ThinBox<T> {
137 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
138 Display::fmt(self.deref(), f)
139 }
140}
141
142#[unstable(feature = "thin_box", issue = "92791")]
143impl<T: ?Sized> Deref for ThinBox<T> {
144 type Target = T;
145
146 fn deref(&self) -> &T {
147 let value = self.data();
148 let metadata = self.meta();
149 let pointer = ptr::from_raw_parts(value as *const (), metadata);
150 unsafe { &*pointer }
151 }
152}
153
154#[unstable(feature = "thin_box", issue = "92791")]
155impl<T: ?Sized> DerefMut for ThinBox<T> {
156 fn deref_mut(&mut self) -> &mut T {
157 let value = self.data();
158 let metadata = self.meta();
159 let pointer = ptr::from_raw_parts_mut::<T>(value as *mut (), metadata);
160 unsafe { &mut *pointer }
161 }
162}
163
164#[unstable(feature = "thin_box", issue = "92791")]
165impl<T: ?Sized> Drop for ThinBox<T> {
166 fn drop(&mut self) {
167 unsafe {
168 let value = self.deref_mut();
169 let value = value as *mut T;
170 self.with_header().drop::<T>(value);
171 }
172 }
173}
174
175#[unstable(feature = "thin_box", issue = "92791")]
176impl<T: ?Sized> ThinBox<T> {
177 fn meta(&self) -> <T as Pointee>::Metadata {
178 unsafe { *self.with_header().header() }
181 }
182
183 fn data(&self) -> *mut u8 {
184 self.with_header().value()
185 }
186
187 fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
188 unsafe { &*((&raw const self.ptr) as *const WithHeader<_>) }
190 }
191}
192
193#[repr(transparent)]
199struct WithHeader<H>(NonNull<u8>, PhantomData<H>);
200
201#[repr(transparent)]
204struct WithOpaqueHeader(NonNull<u8>);
205
206impl WithOpaqueHeader {
207 #[cfg(not(no_global_oom_handling))]
208 fn new<H, T>(header: H, value: T) -> Self {
209 let ptr = WithHeader::new(header, value);
210 Self(ptr.0)
211 }
212
213 #[cfg(not(no_global_oom_handling))]
214 fn new_unsize_zst<Dyn, T>(value: T) -> Self
215 where
216 Dyn: ?Sized,
217 T: Unsize<Dyn>,
218 {
219 let ptr = WithHeader::<<Dyn as Pointee>::Metadata>::new_unsize_zst::<Dyn, T>(value);
220 Self(ptr.0)
221 }
222
223 fn try_new<H, T>(header: H, value: T) -> Result<Self, core::alloc::AllocError> {
224 WithHeader::try_new(header, value).map(|ptr| Self(ptr.0))
225 }
226}
227
228impl<H> WithHeader<H> {
229 #[cfg(not(no_global_oom_handling))]
230 fn new<T>(header: H, value: T) -> WithHeader<H> {
231 let value_layout = Layout::new::<T>();
232 let Ok((layout, value_offset)) = Self::alloc_layout(value_layout) else {
233 alloc::handle_alloc_error(Layout::new::<()>());
240 };
241
242 unsafe {
243 let ptr = if layout.size() == 0 {
247 debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
250 layout.dangling()
251 } else {
252 let ptr = alloc::alloc(layout);
253 if ptr.is_null() {
254 alloc::handle_alloc_error(layout);
255 }
256 let ptr = ptr.add(value_offset) as *mut _;
259
260 NonNull::new_unchecked(ptr)
261 };
262
263 let result = WithHeader(ptr, PhantomData);
264 ptr::write(result.header(), header);
265 ptr::write(result.value().cast(), value);
266
267 result
268 }
269 }
270
271 fn try_new<T>(header: H, value: T) -> Result<WithHeader<H>, core::alloc::AllocError> {
274 let value_layout = Layout::new::<T>();
275 let Ok((layout, value_offset)) = Self::alloc_layout(value_layout) else {
276 return Err(core::alloc::AllocError);
277 };
278
279 unsafe {
280 let ptr = if layout.size() == 0 {
284 debug_assert!(
287 value_offset == 0 && mem::size_of::<T>() == 0 && mem::size_of::<H>() == 0
288 );
289 layout.dangling()
290 } else {
291 let ptr = alloc::alloc(layout);
292 if ptr.is_null() {
293 return Err(core::alloc::AllocError);
294 }
295
296 let ptr = ptr.add(value_offset) as *mut _;
299
300 NonNull::new_unchecked(ptr)
301 };
302
303 let result = WithHeader(ptr, PhantomData);
304 ptr::write(result.header(), header);
305 ptr::write(result.value().cast(), value);
306
307 Ok(result)
308 }
309 }
310
311 #[cfg(not(no_global_oom_handling))]
313 fn new_unsize_zst<Dyn, T>(value: T) -> WithHeader<H>
314 where
315 Dyn: Pointee<Metadata = H> + ?Sized,
316 T: Unsize<Dyn>,
317 {
318 assert!(mem::size_of::<T>() == 0);
319
320 const fn max(a: usize, b: usize) -> usize {
321 if a > b { a } else { b }
322 }
323
324 let alloc: &<Dyn as Pointee>::Metadata = const {
329 let alloc_align =
333 max(mem::align_of::<T>(), mem::align_of::<<Dyn as Pointee>::Metadata>());
334
335 let alloc_size =
336 max(mem::align_of::<T>(), mem::size_of::<<Dyn as Pointee>::Metadata>());
337
338 unsafe {
339 let alloc: *mut u8 = const_allocate(alloc_size, alloc_align);
341
342 let metadata_offset =
343 alloc_size.checked_sub(mem::size_of::<<Dyn as Pointee>::Metadata>()).unwrap();
344 let metadata_ptr: *mut <Dyn as Pointee>::Metadata =
346 alloc.add(metadata_offset).cast();
347 metadata_ptr.write(ptr::metadata::<Dyn>(ptr::dangling::<T>() as *const Dyn));
349
350 &*(metadata_ptr)
352 }
353 };
354
355 let value_ptr =
357 unsafe { (alloc as *const <Dyn as Pointee>::Metadata).add(1) }.cast::<T>().cast_mut();
358 debug_assert!(value_ptr.is_aligned());
359 mem::forget(value);
360 WithHeader(NonNull::new(value_ptr.cast()).unwrap(), PhantomData)
361 }
362
363 unsafe fn drop<T: ?Sized>(&self, value: *mut T) {
367 struct DropGuard<H> {
368 ptr: NonNull<u8>,
369 value_layout: Layout,
370 _marker: PhantomData<H>,
371 }
372
373 impl<H> Drop for DropGuard<H> {
374 fn drop(&mut self) {
375 if self.value_layout.size() == 0 {
377 return;
378 }
379
380 unsafe {
381 let (layout, value_offset) =
383 WithHeader::<H>::alloc_layout(self.value_layout).unwrap_unchecked();
384
385 debug_assert!(layout.size() != 0);
387 alloc::dealloc(self.ptr.as_ptr().sub(value_offset), layout);
388 }
389 }
390 }
391
392 unsafe {
393 let _guard = DropGuard {
395 ptr: self.0,
396 value_layout: Layout::for_value_raw(value),
397 _marker: PhantomData::<H>,
398 };
399
400 ptr::drop_in_place::<T>(value);
403 }
404 }
405
406 fn header(&self) -> *mut H {
407 let hp = unsafe { self.0.as_ptr().sub(Self::header_size()) as *mut H };
415 debug_assert!(hp.is_aligned());
416 hp
417 }
418
419 fn value(&self) -> *mut u8 {
420 self.0.as_ptr()
421 }
422
423 const fn header_size() -> usize {
424 mem::size_of::<H>()
425 }
426
427 fn alloc_layout(value_layout: Layout) -> Result<(Layout, usize), LayoutError> {
428 Layout::new::<H>().extend(value_layout)
429 }
430}
431
432#[unstable(feature = "thin_box", issue = "92791")]
433impl<T: ?Sized + Error> Error for ThinBox<T> {
434 fn source(&self) -> Option<&(dyn Error + 'static)> {
435 self.deref().source()
436 }
437}