#layout #type #const #type-layout #field-name #variant-name

nightly const-type-layout

Derivable const trait to view and compare the layout of a struct, union, or enum

4 releases (2 breaking)

0.3.0 Mar 21, 2024
0.2.1 Dec 24, 2023
0.2.0 Dec 10, 2023
0.1.0 Jan 20, 2023

#349 in Rust patterns

Download history 39/week @ 2023-12-17 88/week @ 2023-12-24 66/week @ 2023-12-31 208/week @ 2024-01-07 69/week @ 2024-01-14 16/week @ 2024-01-21 65/week @ 2024-02-18 52/week @ 2024-02-25 2/week @ 2024-03-03 9/week @ 2024-03-10 130/week @ 2024-03-17 3/week @ 2024-03-24 31/week @ 2024-03-31

173 downloads per month

MIT/Apache

105KB
2K SLoC

CI Status MSRV Latest Version Rust Doc Crate Rust Doc Main License Status Code Coverage Gitpod Ready-to-Code

const-type-layout is a type layout comparison aid, providing a #[derive]able TypeLayout trait that reports:

  • The type's name, size, and minimum alignment
  • The type's structure, i.e. struct vs. union vs. enum
  • Each field's name and offset
  • Each variant's name and discriminant

Through the auto-implemented TypeGraphLayout trait, the deep type layout is also reported as a graph.

This crate heavily builds on the original runtime type-layout crate by Lucien Greathouse.

Examples

The layout of types is only defined if they're #[repr(C)]. This crate works on non-#[repr(C)] types, but their layout is unpredictable.

use const_type_layout::TypeLayout;

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

assert_eq!(
    format!("{:#?}", Foo::TYPE_LAYOUT),
r#"TypeLayoutInfo {
    name: "mycrate::mymodule::Foo",
    size: 8,
    alignment: 4,
    structure: Struct {
        repr: "C",
        fields: [
            Field {
                name: "a",
                offset: Inhabited(
                    0,
                ),
                ty: "u8",
            },
            Field {
                name: "b",
                offset: Inhabited(
                    4,
                ),
                ty: "u32",
            },
        ],
    },
}"#
);

Over-aligned types have trailing padding, which can be a source of bugs in some FFI scenarios:

use const_type_layout::TypeLayout;

#[derive(TypeLayout)]
#[repr(C, align(128))]
struct OverAligned {
    value: u8,
}

assert_eq!(
    format!("{:#?}", OverAligned::TYPE_LAYOUT),
r#"TypeLayoutInfo {
    name: "mycrate::mymodule::OverAligned",
    size: 128,
    alignment: 128,
    structure: Struct {
        repr: "C,align(128)",
        fields: [
            Field {
                name: "value",
                offset: Inhabited(
                    0,
                ),
                ty: "u8",
            },
        ],
    },
}"#
)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~185KB