memory-layout

crates.io docs.rs CI

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

Example

```rust use memorylayout::memorylayout;

[memory_layout]

pub struct Example { #[fieldoffset(0x00)] a: i32, #[fieldoffset(0x10)] b: u64, #[field_offset(0x20)] c: f32 } Will expand to: rust

[repr(C, packed)]

pub struct Example { #[doc(hidden)] _pad0: [u8; 0usize], a: i32, #[doc(hidden)] _pad1: [u8; 16usize - ::core::mem::sizeof::()], b: u64, #[doc(hidden)] _pad2: [u8; 8usize - ::core::mem::size_of::()], c: f32 } ```

Caveats

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.