hioff
The original name is hun_offsetof
. Provides C-like macros: offset_of
and container_of
rust
offset_of!(type, member) -> usize;
container_of!(&obj, type, member) -> &type;
container_of_mut!(&obj, type, member) -> &mut type;
```rust
extern crate hun_offsetof as hun;
struct Bar { key: i32, value: i32, }
struct Foo { key: i32, value: [Bar; 2], }
asserteq!(hun::offsetof!(Bar, value), 4); asserteq!(hun::offsetof!(Foo, value[1].key), 12);
let foo = Foo { key: 1, value: [ Bar { key: 2, value: 2}, Bar { key: 3, value: 3 }], }; let value = &foo.value[1].value;
let obj = unsafe { hun::containerof!(value, Foo, value[1].value) }; asserteq!(obj as *const _, &foo as *const _); ```