Macro core::mem::offset_of

source ·
pub macro offset_of($Container:ty, $($fields:tt).+ $(,)?) {
    ...
}
🔬This is a nightly-only experimental API. (offset_of #106655)
Expand description

Expands to the offset in bytes of a field from the beginning of the given type.

Only structs, unions and tuples are supported.

Nested field accesses may be used, but not array indexes like in C’s offsetof.

Note that the output of this macro is not stable, except for #[repr(C)] types.

Examples

#![feature(offset_of)]

use std::mem;
#[repr(C)]
struct FieldStruct {
    first: u8,
    second: u16,
    third: u8
}

assert_eq!(mem::offset_of!(FieldStruct, first), 0);
assert_eq!(mem::offset_of!(FieldStruct, second), 2);
assert_eq!(mem::offset_of!(FieldStruct, third), 4);

#[repr(C)]
struct NestedA {
    b: NestedB
}

#[repr(C)]
struct NestedB(u8);

assert_eq!(mem::offset_of!(NestedA, b.0), 0);
Run