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
| Property | Type |
|---|---|
size | u64 |
alignment | u64 |
Constructors
static new(size: u64, alignment: u64) -> Layout;
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;
static of<T>() -> Layout;Layout of a single value of type T.
static array<T>(count: u64) -> Layout;
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>;
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;
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.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Layout {
size: u64;
alignment: u64;
}Layout: size + alignment of a memory block.
Layout.size: u64Layout.alignment: u64static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Layout::new(size: u64, alignment: u64) -> LayoutConstruct 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.
(parameter) size: u64(parameter) alignment: u64type struct Layout {
size: u64;
alignment: u64;
}Layout: size + alignment of a memory block.
std::alloc::layoutstatic Layout::of<T>() -> LayoutLayout of a single value of type T.
type parameter Tstatic Layout::array<T>(count: u64) -> LayoutLayout 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.
(parameter) count: u64static Layout::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.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
Layout::padded_size(&this) -> u64size rounded up to the next multiple of alignment. The stride to use when packing adjacent values of this layout in memory.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorLayout.size: u64Layout.alignment: u64Layout::padded_size(&this) -> u64size rounded up to the next multiple of alignment. The stride to use when packing adjacent values of this layout in memory.
static Layout::new(size: u64, alignment: u64) -> LayoutConstruct 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 Layout::array<T>(count: u64) -> LayoutLayout 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 Layout::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.
Option::NoneResult::Err(E)type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}