1 unstable release
Uses new Rust 2024
0.0.2 | Feb 26, 2025 |
---|---|
0.0.1 |
|
#562 in Procedural macros
282 downloads per month
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:
- Accessing field pointers:
- Retrieving the field count: const_field_count
Dependencies
~200–630KB
~15K SLoC