silk/layout
Checked size-and-alignment descriptions used to request storage from an allocator.
When to use
Prefer of when storage will hold a known Silk type. Use make only at an untyped boundary
that already knows a byte size and alignment, and repeat when sizing contiguous elements.
Details
Alignments are non-zero powers of two. repeat preserves the element alignment and reports
LayoutOverflow instead of wrapping the aggregate byte size. A Layout describes storage;
it does not allocate or initialize it.
Examples
Size storage for repeated values
import silk.layout as Layout
import silk.usize as usize
pub fn main() -> i32 {
let element = Layout.of<i32>()
return match move Layout.repeat(move element, 3) {
Layout.Layout {bytes, alignment} => usize.toI32(bytes) + 30
Layout.LayoutOverflow {} => 0
}
}Import as Layout with import silk.layout.
Public declarations: 6.
Layout
pub struct LayoutA byte size paired with a non-zero power-of-two alignment.
Field bytes
pub bytes: usizeThe number of addressable bytes required by the described storage.
Field alignment
pub alignment: usizeThe required non-zero power-of-two byte alignment.
InvalidAlignment
pub struct InvalidAlignmentReports the rejected alignment supplied to make.
Field alignment
pub alignment: usizeThe zero or non-power-of-two alignment that was rejected.
LayoutOverflow
pub struct LayoutOverflowReports that repeat could not represent the aggregate byte size as usize.
of
pub fn of<T>() -> LayoutReturns the byte size and alignment required to store one value of T.
make
pub fn make(size: usize, alignment: usize) -> silk/layout.InvalidAlignment | silk/layout.LayoutCreates a layout from an explicit byte size and alignment.
Gotchas
If alignment is zero or is not a power of two, returns InvalidAlignment with that value.
repeat
pub fn repeat(layout: Layout, count: usize) -> silk/layout.Layout | silk/layout.LayoutOverflowReturns a layout for count contiguous instances without wrapping the total byte size.
Details
The result keeps the input alignment and multiplies its byte size by count.
Gotchas
If the total byte size does not fit in usize, returns LayoutOverflow.