| Crates.io | const-type-layout |
| lib.rs | const-type-layout |
| version | 0.3.2 |
| created_at | 2023-01-20 08:11:50.39946+00 |
| updated_at | 2024-09-23 08:12:21.155045+00 |
| description | Derivable const trait to view and compare the layout of a struct, union, or enum. |
| homepage | https://github.com/juntyr/const-type-layout |
| repository | https://github.com/juntyr/const-type-layout |
| max_upload_size | |
| id | 763214 |
| size | 117,291 |
const-type-layout is a type layout comparison aid, providing a #[derive]able TypeLayout trait
that reports:
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.
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",
},
],
},
}"#
)
Licensed under either of
at your option.
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.