| 12 | /// You can initialize the resulting box's value via [`Box::write`]. |
| 13 | #[inline] |
| 14 | fn new_uninit_box<T>() -> Result<Box<MaybeUninit<T>>, OutOfMemory> { |
| 15 | let layout = Layout::new::<MaybeUninit<T>>(); |
| 16 | |
| 17 | if layout.size() == 0 { |
| 18 | // NB: no actual allocation takes place when boxing zero-sized |
| 19 | // types. |
| 20 | return Ok(Box::new(MaybeUninit::uninit())); |
| 21 | } |
| 22 | |
| 23 | // Safety: layout size is non-zero. |
| 24 | let ptr = unsafe { try_alloc(layout)? }; |
| 25 | |
| 26 | let ptr = ptr.cast::<MaybeUninit<T>>(); |
| 27 | |
| 28 | // Safety: The pointer's memory block was allocated by the global allocator. |
| 29 | Ok(unsafe { Box::from_raw(ptr.as_ptr()) }) |
| 30 | } |
| 31 | |
| 32 | impl<T> TryNew for Box<T> { |
| 33 | type Value = T; |