MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / new_uninit_box

Function new_uninit_box

crates/core/src/alloc/boxed.rs:14–30  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12/// You can initialize the resulting box's value via [`Box::write`].
13#[inline]
14fn 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
32impl<T> TryNew for Box<T> {
33 type Value = T;

Callers

nothing calls this directly

Calls 5

OkFunction · 0.85
newFunction · 0.50
try_allocFunction · 0.50
sizeMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected