]> git.proxmox.com Git - rustc.git/blob - vendor/memoffset-0.2.1/src/lib.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / vendor / memoffset-0.2.1 / src / lib.rs
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 //!
23 //! Some of the funcationality of the crate makes no sense when used along with structs that
24 //! are not `#[repr(C, packed)]`, but it is up to the user to make sure that they are.
25 //!
26 //! ## Examples
27 //! ```
28 //! #[macro_use]
29 //! extern crate memoffset;
30 //!
31 //! #[repr(C, packed)]
32 //! struct HelpMeIAmTrappedInAStructFactory {
33 //! help_me_before_they_: [u8; 15],
34 //! a: u32
35 //! }
36 //!
37 //! fn main() {
38 //! assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15);
39 //! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19);
40 //! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_[2] .. a), 2..15);
41 //! }
42 //! ```
43 //!
44 //! This functionality can be useful, for example, for checksum calculations:
45 //!
46 //! ```ignore
47 //! #[repr(C, packed)]
48 //! struct Message {
49 //! header: MessageHeader,
50 //! fragment_index: u32,
51 //! fragment_count: u32,
52 //! payload: [u8; 1024],
53 //! checksum: u16
54 //! }
55 //!
56 //! let checksum_range = &raw[span_of!(Message, header..checksum)];
57 //! let checksum = crc16(checksum_range);
58 //! ```
59
60 #![no_std]
61
62 // This `use` statement enables the macros to use `$crate::mem`.
63 // Doing this enables this crate to function under both std and no-std crates.
64 #[doc(hidden)]
65 pub use core::mem;
66
67 #[macro_use]
68 mod offset_of;
69 #[macro_use]
70 mod span_of;