pub fn alloc_for_layout(layout: Layout) -> PtrUninitExpand description
Allocates memory for a layout, correctly handling zero-sized types.
For ZSTs (zero-sized types), returns a dangling but properly aligned pointer
without actually allocating. This avoids undefined behavior since
alloc::alloc::alloc with a zero-sized layout is UB.
§Returns
A PtrUninit pointing to:
- Newly allocated memory for non-zero-sized layouts
- A dangling, aligned pointer for zero-sized layouts
§Panics
Panics if allocation fails (calls handle_alloc_error).
§Example
ⓘ
use core::alloc::Layout;
use facet_core::{alloc_for_layout, dealloc_for_layout};
let layout = Layout::new::<u32>();
let ptr = alloc_for_layout(layout);
// ... use ptr ...
unsafe { dealloc_for_layout(ptr.assume_init(), layout); }