3 [![](https://img.shields.io/crates/v/memoffset.svg)](https://crates.io/crates/memoffset)
5 C-Like `offset_of` functionality for Rust structs.
7 Introduces the following macros:
8 * `offset_of!` for obtaining the offset of a member of a struct.
9 * `offset_of_tuple!` for obtaining the offset of a member of a tuple. (Requires Rust 1.20+)
10 * `span_of!` for obtaining the range that a field, or fields, span.
12 `memoffset` works under `no_std` environments.
15 Add the following dependency to your `Cargo.toml`:
22 These versions will compile fine with rustc versions greater or equal to 1.19.
26 use memoffset::{offset_of, span_of};
37 assert_eq!(offset_of!(Foo, b), 4);
38 assert_eq!(offset_of!(Foo, d), 4+4+5);
40 assert_eq!(span_of!(Foo, a), 0..4);
41 assert_eq!(span_of!(Foo, a .. c), 0..8);
42 assert_eq!(span_of!(Foo, a ..= c), 0..13);
43 assert_eq!(span_of!(Foo, ..= d), 0..17);
44 assert_eq!(span_of!(Foo, b ..), 4..17);
50 ### Usage in constants ###
51 `memoffset` has **experimental** support for compile-time `offset_of!` on a nightly compiler.
53 In order to use it, you must enable the `unstable_const` crate feature and several compiler features.
57 [dependencies.memoffset]
59 features = ["unstable_const"]
62 Your crate root: (`lib.rs`/`main.rs`)
64 #![feature(const_ptr_offset_from, const_refs_to_cell)]