stable-type-layout 0.0.31

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

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.