Macro mac::addrs_of [] [src]

macro_rules! addrs_of {
    ($obj:expr => $($field:ident),+) => { ... };
}

Make a tuple of the addresses of some of a struct's fields.

This is useful when you are transmuting between struct types and would like an additional dynamic check that the layouts match. It's difficult to make such an assertion statically in Rust at present.

Example

use std::mem;

struct Foo { x: i32, y: i32 }
struct Bar { x: u32, y: u32 }

let foo = Foo { x: 3, y: 4 };
let old_addrs = addrs_of!(foo => x, y);

let bar = unsafe {
    mem::transmute::<&Foo, &Bar>(&foo)
};
let new_addrs = addrs_of!(bar => x, y);
assert_eq!(old_addrs, new_addrs);

assert_eq!(bar.x, 3);
assert_eq!(bar.y, 4);