Crates.io | type-layout |
lib.rs | type-layout |
version | 0.2.0 |
source | src |
created_at | 2020-09-13 02:50:28.54772 |
updated_at | 2020-09-19 05:55:17.763165 |
description | Derivable trait to view the layout of a struct, useful for debugging. |
homepage | https://github.com/LPGhatguy/type-layout |
repository | https://github.com/LPGhatguy/type-layout |
max_upload_size | |
id | 287998 |
size | 23,711 |
type-layout is a type layout debugging aid, providing a #[derive]
able trait
that reports:
type-layout currently only functions on structs with named fields. This is a temporary limitation.
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 type_layout::TypeLayout;
#[derive(TypeLayout)]
#[repr(C)]
struct Foo {
a: u8,
b: u32,
}
println!("{}", Foo::type_layout());
// prints:
// Foo (size 8, alignment 4)
// | Offset | Name | Size |
// | ------ | --------- | ---- |
// | 0 | a | 1 |
// | 1 | [padding] | 3 |
// | 4 | b | 4 |
Over-aligned types have trailing padding, which can be a source of bugs in some FFI scenarios:
use type_layout::TypeLayout;
#[derive(TypeLayout)]
#[repr(C, align(128))]
struct OverAligned {
value: u8,
}
println!("{}", OverAligned::type_layout());
// prints:
// OverAligned (size 128, alignment 128)
// | Offset | Name | Size |
// | ------ | --------- | ---- |
// | 0 | value | 1 |
// | 1 | [padding] | 127 |
type-layout supports Rust 1.34.1 and newer. Until type-layout reaches 1.0, changes to the MSRV will require major version bumps. After 1.0, MSRV changes will only require minor version bumps, but will need significant justification.
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.