Skip to main content

Crate stable_type_layout

Crate stable_type_layout 

Source
Expand description

Layout descriptors for struct or enum types with an inventory registry.

TypeLayout is derivable and exposes a const TypeLayoutInfo holding a type’s name, size, minimum alignment, field offsets and enum variant discriminants.

use stable_type_layout::{Field, TypeLayout, TypeStructure};

#[derive(TypeLayout)]
#[repr(C)]
struct Foo {
    a: u8,
    b: u32,
}

const LAYOUT: stable_type_layout::TypeLayoutInfo = <Foo as TypeLayout>::TYPE_LAYOUT;
assert_eq!(LAYOUT.name, "Foo");
assert_eq!((LAYOUT.size, LAYOUT.align), (8, 4));
assert_eq!(
    LAYOUT.structure,
    TypeStructure::Struct { fields: &[Field { name: "a", offset: 0 }, Field { name: "b", offset: 4 }] }
);

Types can additionally register! themselves into a crate-wide inventory registry, which can be used for snapshot testing, so a field, variant reorder, or a size change can be caught.

Re-exports§

pub use inventory;

Macros§

register
Registers a type’s TypeLayoutInfo with the registry read by all and render.

Structs§

Field
A field’s name and byte offset within its type.
TypeLayoutInfo
The concrete memory layout of a type: its name, size, minimum alignment and TypeStructure.
Variant
An enum variant’s name, declaration index and discriminant.

Enums§

TypeStructure
Whether a type is a struct, union or enum, and the fields or variants it holds.

Traits§

TypeLayout
A type whose memory layout is known at compile time.

Functions§

all
Every registered TypeLayoutInfo, sorted by type name (deterministic order for snapshotting).
render
Render every registered layout as a stable, human-readable string for snapshot assertions.

Derive Macros§

TypeLayout
Implements TypeLayout, recording the type’s size, alignment, field offsets and enum variant discriminants as a const.