Skip to content
CryoCryo home
Stdliballoc

layout

import std::alloc::layout; · source

Types

Layout

type struct Layout {
    size:      u64;
    alignment: u64;

    static new(size: u64, alignment: u64) -> Layout;
    static of<T>() -> Layout;
    static array<T>(count: u64) -> Layout;
    static try_array<T>(count: u64) -> Option<Layout>;
    size(&this) -> u64;
    alignment(&this) -> u64;
    padded_size(&this) -> u64;
}

Every Allocator call takes a Layout: a size and an alignment. Constructing one validates that the alignment is a non-zero power of two, so allocators downstream can rely on the invariant without re-checking it.

size() and alignment() read the two fields back; padded_size() is the stride to use when packing adjacent values.

Properties

PropertyType
sizeu64
alignmentu64

Constructors

static new(size: u64, alignment: u64) -> Layout;

Construct from raw size and alignment. Panics if alignment is zero or not a power of two; that's a caller bug, not a runtime condition to recover from. Size of zero is allowed; the allocator decides how to treat it.

static of<T>() -> Layout;

Layout of a single value of type T.

static array<T>(count: u64) -> Layout;

Layout of count contiguous values of type T. Panics on multiplication overflow; the caller asked for more bytes than u64 can describe, which no allocator can satisfy.

static try_array<T>(count: u64) -> Option<Layout>;

Non-panicking array: returns None on multiplication overflow instead of aborting. Lets try_* constructors surface the failure as Err and honor the "try_ never panics" contract. Returns Option (not Result<_, AllocError>) to keep this module free of an alloc::layout <-> alloc::allocator import cycle; the caller maps None to its own error type.

Methods

size(&this) -> u64;
alignment(&this) -> u64;
padded_size(&this) -> u64;

size rounded up to the next multiple of alignment. The stride to use when packing adjacent values of this layout in memory.