Macro memoffset::offset_of

source ·
macro_rules! offset_of {
    ($parent:path, $field:tt) => { ... };
}
Expand description

Calculates the offset of the specified field from the start of the named struct.

Examples

use memoffset::offset_of;

#[repr(C, packed)]
struct Foo {
    a: u32,
    b: u64,
    c: [u8; 5]
}

fn main() {
    assert_eq!(offset_of!(Foo, a), 0);
    assert_eq!(offset_of!(Foo, b), 4);
}

Notes

Rust’s ABI is unstable, and type layout can be changed with each compilation.

Using offset_of! with a repr(Rust) struct will return the correct offset of the specified field for a particular compilation, but the exact value may change based on the compiler version, concrete struct type, time of day, or rustc’s mood.

As a result, the value should not be retained and used between different compilations.