memory-layout
memory-layout is a minimal no_std compatible crate that allows you to specify the memory layout of a struct, similarly to C#'s [StructLayout(LayoutKind.Explicit)].
Docs
https://docs.rs/memory-layout/
Features
- Specify the offset a field should have in a struct.
- Offsets are checked to be valid at compile time.
no_stdcompatible.
Example
use memory_layout;
Will expand to:
Caveats
- Fields have to be defined in ascending order by the specified offset.
#[memory_layout]attribute has to be defined before anyderiveattributes.repr(C, packed)is forced.- Does not ensure valid field alignment (more info).
Comparable projects
struct_layout
This project has a similar goal to this crate, replicating C#'s [StructLayout(LayoutKind.Explicit)]. The key difference is that struct_layout uses an internal array that can be accessed using methods like get_<field_name> and set_<field_name> while this crate aligns the fields themselves.