]>
Commit | Line | Data |
---|---|---|
416331ca XL |
1 | # memoffset # |
2 | ||
3 | [![](http://meritbadge.herokuapp.com/memoffset)](https://crates.io/crates/memoffset) | |
4 | ||
5 | C-Like `offset_of` functionality for Rust structs. | |
6 | ||
7 | Introduces the following macros: | |
8 | * `offset_of!` for obtaining the offset of a member of a struct. | |
9 | * `span_of!` for obtaining the range that a field, or fields, span. | |
10 | ||
11 | `memoffset` works under `no_std` environments. | |
12 | ||
13 | ## Usage ## | |
14 | Add the following dependency to your `Cargo.toml`: | |
15 | ||
16 | ```toml | |
17 | [dependencies] | |
18 | memoffset = "0.2" | |
19 | ``` | |
20 | ||
21 | Add the following lines at the top of your `main.rs` or `lib.rs` files. | |
22 | ||
23 | ```rust | |
24 | #[macro_use] | |
25 | extern crate memoffset; | |
26 | ``` | |
27 | ||
28 | ## Examples ## | |
29 | ```rust | |
30 | #[repr(C, packed)] | |
31 | struct Foo { | |
32 | a: u32, | |
33 | b: u32, | |
34 | c: [u8; 5], | |
35 | d: u32, | |
36 | } | |
37 | ||
38 | assert_eq!(offset_of!(Foo, b), 4); | |
39 | assert_eq!(offset_of!(Foo, c[3]), 11); | |
40 | ||
41 | assert_eq!(span_of!(Foo, a), 0..4); | |
42 | assert_eq!(span_of!(Foo, a .. c), 0..8); | |
43 | assert_eq!(span_of!(Foo, a .. c[1]), 0..9); | |
44 | assert_eq!(span_of!(Foo, a ..= c[1]), 0..10); | |
45 | assert_eq!(span_of!(Foo, ..= d), 0..14); | |
46 | assert_eq!(span_of!(Foo, b ..), 4..17); | |
47 | ``` |