]>
Commit | Line | Data |
---|---|---|
2c00a5a8 XL |
1 | // Copyright (c) 2017 Gilad Naaman |
2 | // | |
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
4 | // of this software and associated documentation files (the "Software"), to deal | |
5 | // in the Software without restriction, including without limitation the rights | |
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
7 | // copies of the Software, and to permit persons to whom the Software is | |
8 | // furnished to do so, subject to the following conditions: | |
9 | // | |
10 | // The above copyright notice and this permission notice shall be included in all | |
11 | // copies or substantial portions of the Software. | |
12 | // | |
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
19 | // SOFTWARE. | |
20 | ||
21 | //! A crate used for calculating offsets of struct members and their spans. | |
22 | //! | |
f035d41b | 23 | //! This functionality currently can not be used in compile time code such as `const` or `const fn` definitions. |
416331ca | 24 | //! |
2c00a5a8 XL |
25 | //! ## Examples |
26 | //! ``` | |
27 | //! #[macro_use] | |
28 | //! extern crate memoffset; | |
29 | //! | |
30 | //! #[repr(C, packed)] | |
31 | //! struct HelpMeIAmTrappedInAStructFactory { | |
32 | //! help_me_before_they_: [u8; 15], | |
33 | //! a: u32 | |
34 | //! } | |
35 | //! | |
36 | //! fn main() { | |
37 | //! assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15); | |
38 | //! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); | |
416331ca | 39 | //! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15); |
2c00a5a8 XL |
40 | //! } |
41 | //! ``` | |
42 | //! | |
43 | //! This functionality can be useful, for example, for checksum calculations: | |
44 | //! | |
45 | //! ```ignore | |
46 | //! #[repr(C, packed)] | |
47 | //! struct Message { | |
48 | //! header: MessageHeader, | |
49 | //! fragment_index: u32, | |
50 | //! fragment_count: u32, | |
51 | //! payload: [u8; 1024], | |
52 | //! checksum: u16 | |
53 | //! } | |
54 | //! | |
55 | //! let checksum_range = &raw[span_of!(Message, header..checksum)]; | |
56 | //! let checksum = crc16(checksum_range); | |
57 | //! ``` | |
58 | ||
59 | #![no_std] | |
f035d41b XL |
60 | #![cfg_attr( |
61 | feature = "unstable_const", | |
62 | feature( | |
63 | ptr_offset_from, | |
5869c6ff | 64 | const_fn, |
f035d41b | 65 | const_ptr_offset_from, |
5869c6ff | 66 | const_maybe_uninit_as_ptr, |
f035d41b XL |
67 | const_raw_ptr_deref, |
68 | ) | |
69 | )] | |
70 | #![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))] | |
71 | ||
72 | #[macro_use] | |
73 | #[cfg(doctests)] | |
74 | #[cfg(doctest)] | |
75 | extern crate doc_comment; | |
76 | #[cfg(doctests)] | |
77 | #[cfg(doctest)] | |
78 | doctest!("../README.md"); | |
2c00a5a8 XL |
79 | |
80 | // This `use` statement enables the macros to use `$crate::mem`. | |
81 | // Doing this enables this crate to function under both std and no-std crates. | |
82 | #[doc(hidden)] | |
83 | pub use core::mem; | |
416331ca XL |
84 | #[doc(hidden)] |
85 | pub use core::ptr; | |
86 | ||
f035d41b XL |
87 | #[macro_use] |
88 | mod raw_field; | |
2c00a5a8 XL |
89 | #[macro_use] |
90 | mod offset_of; | |
91 | #[macro_use] | |
92 | mod span_of; |