type-layout-syn2

Crates.iotype-layout-syn2
lib.rstype-layout-syn2
version0.2.1
sourcesrc
created_at2024-05-16 13:40:06.590914
updated_at2024-05-16 13:56:28.372459
descriptionDerivable trait to view the layout of a struct, useful for debugging. Patched for syn 2.
homepagehttps://github.com/LPGhatguy/type-layout
repositoryhttps://github.com/stelzo/type-layout
max_upload_size
id1242077
size24,448
stelzo (stelzo)

documentation

https://docs.rs/type-layout

README

[!NOTE]
This is only a patched version of the original type-layout for using syn 2 instead of syn 1. This crate will be yanked when the original author applied the patch. See the PR for progress on this matter. The patch contains following changes:

  • Use syn v2 as dependency instead of syn v1.0.40.
  • Use memoffset v0.9 instead of v0.5 for fixing a known bug in memoffset where uninitialized memory could be read.
  • Update Rust MSRV to 1.60 (inherited from syn 2).
  • Change Field struct to enum in TypeLayoutInfo from published source to be feature-synced with v0.2.0 on crates.io.

Use this patch like this type-layout = { version = "0.2", package = "type-layout-syn2" }. While the changes are technically breaking, the original author will decide the actual version.

type-layout

GitHub CI Status type-layout on crates.io type-layout docs

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

  • The type's name, size, and minimum alignment
  • Each field's name, type, offset, and size
  • Padding due to alignment requirements

type-layout currently only functions on structs with named fields. This is a temporary limitation.

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 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  |

Minimum Supported Rust Version (MSRV)

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.

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.

Commit count: 15

cargo fmt