Layout
Describes one target-qualified raw-storage byte size and alignment.
Module std.memory
import std.memory;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Layout
pub struct LayoutDescribes one target-qualified raw-storage byte size and alignment.
Layout is an opaque Copyable compiler-known value. default(Layout) is the valid zero-byte, alignment-one layout. Other values come only from the checked factories, so callers cannot construct a malformed pair.
Example
module example.layout;
from std.memory import Layout;
fn Main() -> i32 {
let Element = Layout.Of<u64>();
if Element.ByteSize == 0 || Element.Alignment == 0 {
return 1;
}
switch Layout.TryCreate(Element.ByteSize, Element.Alignment) {
case .Ok { Value = Exact } => {
if Exact.ByteSize != Element.ByteSize ||
Exact.Alignment != Element.Alignment {
return 2;
}
}
case .Err { Error = _ } => return 3;
}
let ArrayResult = Layout.ArrayOf<u64>(8);
switch ArrayResult {
case .Ok { Value = ArrayLayout } => {
if ArrayLayout.ByteSize == Element.ByteSize * 8 &&
ArrayLayout.Alignment == Element.Alignment {
return 0;
}
return 4;
}
case .Err { Error = _ } => return 5;
}
}Layout.ByteSize
pub ByteSize: usize;Returns the exact target byte extent represented by this value. Return: A usize count; zero is valid and does not imply a null allocation.
Layout.Alignment
pub Alignment: usize;Returns the required nonzero power-of-two target alignment. Return: The alignment in bytes; default(Layout) returns one.
Layout.fn
pub static const fn TryCreate(ByteSize: usize, Alignment: usize)
-> Result<Layout, AllocationError>Validates and constructs one raw-storage layout. ByteSize: Requested byte extent, including zero. Alignment: Required nonzero power-of-two byte alignment. Return: The exact layout, or InvalidLayout for an invalid alignment.
Layout.fn
pub static const fn Of<T>() -> LayoutReturns the selected complete-object layout of T. T: Complete sized type whose target layout is requested. Return: Positive complete-object size and required alignment for T.
Layout.fn
pub static const fn ArrayOf<T>(Count: usize)
-> Result<Layout, AllocationError>Computes storage for Count contiguous T slots. T: Complete sized element type. Count: Number of raw slots; zero preserves alignof(T). Return: The array layout or SizeOverflow when multiplication overflows.