]> git.proxmox.com Git - rustc.git/blame - vendor/memoffset-0.2.1/README.md
New upstream version 1.40.0+dfsg1
[rustc.git] / vendor / memoffset-0.2.1 / README.md
CommitLineData
416331ca
XL
1# memoffset #
2
3[![](http://meritbadge.herokuapp.com/memoffset)](https://crates.io/crates/memoffset)
4
5C-Like `offset_of` functionality for Rust structs.
6
7Introduces 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 ##
14Add the following dependency to your `Cargo.toml`:
15
16```toml
17[dependencies]
18memoffset = "0.2"
19```
20
21Add the following lines at the top of your `main.rs` or `lib.rs` files.
22
23```rust
24#[macro_use]
25extern crate memoffset;
26```
27
28## Examples ##
29```rust
30#[repr(C, packed)]
31struct Foo {
32 a: u32,
33 b: u32,
34 c: [u8; 5],
35 d: u32,
36}
37
38assert_eq!(offset_of!(Foo, b), 4);
39assert_eq!(offset_of!(Foo, c[3]), 11);
40
41assert_eq!(span_of!(Foo, a), 0..4);
42assert_eq!(span_of!(Foo, a .. c), 0..8);
43assert_eq!(span_of!(Foo, a .. c[1]), 0..9);
44assert_eq!(span_of!(Foo, a ..= c[1]), 0..10);
45assert_eq!(span_of!(Foo, ..= d), 0..14);
46assert_eq!(span_of!(Foo, b ..), 4..17);
47```