#offset #ffi #struct #struct-fields #field-name #retrieve #procedural

macro struct-field-offsets

Procedural macro to retrieve field names and offsets from a struct

1 unstable release

Uses new Rust 2024

0.0.2 Feb 26, 2025
0.0.1 Feb 26, 2025

#562 in Procedural macros

Download history 199/week @ 2025-02-21 83/week @ 2025-02-28

282 downloads per month

MIT/Apache

8KB

struct-field-offsets

This crate provides a field_offsets member over struct declaration that adorns the FieldOffsets macro.

For example, this can be used in FFI scenarios where asserting the offset of each field among the various languages struct becomes a concern.

use struct_field_offsets::FieldOffsets;

// at declaration site
#[derive(FieldOffsets)]
#[repr(C)]
struct Data {
    x: i32,
    y: i32,
    label: [u8;8]
}

// in the code
let offsets = Data::field_offsets();
for (name,offset) in offsets {
    println!("field {name} offset is {offset}.");
}
// prints:
// > field x offset is 0.
// > field y offset is 4.
// > field label offset is 8.

In your Cargo.toml:

[dependencies]
struct-field-offsets = "*"

Similar crates:

Dependencies

~200–630KB
~15K SLoC