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)]
.
https://docs.rs/memory-layout/
no_std
compatible.```rust use memorylayout::memorylayout;
pub struct Example {
#[fieldoffset(0x00)]
a: i32,
#[fieldoffset(0x10)]
b: u64,
#[field_offset(0x20)]
c: f32
}
Will expand to:
rust
pub struct Example {
#[doc(hidden)]
_pad0: [u8; 0usize],
a: i32,
#[doc(hidden)]
_pad1: [u8; 16usize - ::core::mem::sizeof::
#[memory_layout]
attribute has to be defined before any derive
attributes.repr(C, packed)
is forced.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.