4

C has offsetof, is there some way to do this in Rust?

Even though the order may change between compilations (unless repr(C) is used), for a given complication it should be possible to access the offset of a struct member at compile time.

Whats the best way find this offset in Rust?

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • If having to create an instance of the struct is acceptable, you can do `&foo.bar as *const _ as usize - &foo as *const _ as usize` to get the offset of the field `bar`. – Dogbert Oct 28 '16 at 17:11
  • 3
    The question this is marked as a duplicate of doesn't provide a convenient replacement for `offsetof`, nor should it - since its asking a related but different question. – ideasman42 Jan 15 '17 at 08:07

0 Answers0