Attribute Macro memory_layout

Source
#[memory_layout]
Expand description

Allows for field_offsets to be defined in the struct. All fields in the struct have to be annotated with a field_offset attribute, and must be defined in-order. A field_offset attribute has to include a int literal, which indicates the offset the field should have.

The macro will also add repr(C, packed) to the struct it’s applied to.

Warning: The attribute has to be defined before any derive attributes.

§Example

use ::memory_layout_codegen::memory_layout;

#[memory_layout(0x38)]
pub struct Example {
  #[field_offset(0x00)]
  a: i32,
  #[field_offset(0x10)]
  b: u64,
  #[field_offset(0x18)]
  c: f32
}

Will expand to:

pub struct Example {
  #[doc(hidden)]
  __pad0: [u8; 0usize],
  a:      i32,
  #[doc(hidden)]
  __pad1: [u8; 16usize - ::core::mem::size_of::<i32>()],
  b:      u64,
  #[doc(hidden)]
  __pad2: [u8; 8usize - ::core::mem::size_of::<u64>()],
  c:      f32
  __pad3: [u8; 8usize - ::core::mem::size_of::<u64>()],
}