https://github.com/juntyr/const-type-layout
A derive trait which enables the const comparison of type layouts
https://github.com/juntyr/const-type-layout
const-evaluation const-generics rust type-layout
Last synced: about 2 months ago
JSON representation
A derive trait which enables the const comparison of type layouts
- Host: GitHub
- URL: https://github.com/juntyr/const-type-layout
- Owner: juntyr
- License: other
- Created: 2021-10-01T08:24:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-26T18:59:44.000Z (6 months ago)
- Last Synced: 2025-03-18T09:21:58.294Z (2 months ago)
- Topics: const-evaluation, const-generics, rust, type-layout
- Language: Rust
- Homepage: https://crates.io/crates/const-type-layout
- Size: 5.03 MB
- Stars: 3
- Watchers: 0
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
[![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] [![License Status]][fossa] [![Code Coverage]][codecov]
[CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/const-type-layout/ci.yml?branch=main
[workflow]: https://github.com/juntyr/const-type-layout/actions/workflows/ci.yml?query=branch%3Amain[MSRV]: https://img.shields.io/badge/MSRV-1.78.0--nightly-orange
[repo]: https://github.com/juntyr/const-type-layout[Latest Version]: https://img.shields.io/crates/v/const-type-layout
[crates.io]: https://crates.io/crates/const-type-layout[Rust Doc Crate]: https://img.shields.io/docsrs/const-type-layout
[docs.rs]: https://docs.rs/const-type-layout/[Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
[docs]: https://juntyr.github.io/const-type-layout/const_type_layout[License Status]: https://app.fossa.com/api/projects/custom%2B26490%2Fgithub.com%2Fjuntyr%2Fconst-type-layout.svg?type=shield
[fossa]: https://app.fossa.com/projects/custom%2B26490%2Fgithub.com%2Fjuntyr%2Fconst-type-layout?ref=badge_shield[Code Coverage]: https://img.shields.io/codecov/c/github/juntyr/const-type-layout?token=J39WVBIMZX
[codecov]: https://codecov.io/gh/juntyr/const-type-layout`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 discriminantThrough 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](https://github.com/LPGhatguy/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.```rust
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:```rust
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
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)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.