()
| 26 | /// Create a `Box<T>`, or return an `OutOfMemory` error. |
| 27 | #[inline] |
| 28 | pub(crate) fn try_new_uninit_box<T>() -> Result<Box<MaybeUninit<T>>, OutOfMemory> { |
| 29 | let layout = std_alloc::alloc::Layout::new::<MaybeUninit<T>>(); |
| 30 | |
| 31 | if layout.size() == 0 { |
| 32 | // NB: no actual allocation takes place when boxing zero-sized types. |
| 33 | return Ok(Box::new(MaybeUninit::uninit())); |
| 34 | } |
| 35 | |
| 36 | // Safety: layout size is non-zero. |
| 37 | let ptr = unsafe { try_alloc(layout)? }; |
| 38 | |
| 39 | let ptr = ptr.cast::<MaybeUninit<T>>(); |
| 40 | |
| 41 | // Safety: The pointer's memory block was allocated by the global allocator. |
| 42 | Ok(unsafe { Box::from_raw(ptr.as_ptr()) }) |
| 43 | } |
no test coverage detected