| 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 |
//! This functionality currently can not be used in compile time code such as `const` or `const fn` definitions. |
| 24 |
//! |
| 25 |
//! ## Examples |
| 26 |
//! ``` |
| 27 |
//! use memoffset::{offset_of, span_of}; |
| 28 |
//! |
| 29 |
//! #[repr(C, packed)] |
| 30 |
//! struct HelpMeIAmTrappedInAStructFactory { |
| 31 |
//! help_me_before_they_: [u8; 15], |
| 32 |
//! a: u32 |
| 33 |
//! } |
| 34 |
//! |
| 35 |
//! fn main() { |
| 36 |
//! assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15); |
| 37 |
//! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); |
| 38 |
//! assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15); |
| 39 |
//! } |
| 40 |
//! ``` |
| 41 |
//! |
| 42 |
//! This functionality can be useful, for example, for checksum calculations: |
| 43 |
//! |
| 44 |
//! ```ignore |
| 45 |
//! #[repr(C, packed)] |
| 46 |
//! struct Message { |
| 47 |
//! header: MessageHeader, |
| 48 |
//! fragment_index: u32, |
| 49 |
//! fragment_count: u32, |
| 50 |
//! payload: [u8; 1024], |
| 51 |
//! checksum: u16 |
| 52 |
//! } |
| 53 |
//! |
| 54 |
//! let checksum_range = &raw[span_of!(Message, header..checksum)]; |
| 55 |
//! let checksum = crc16(checksum_range); |
| 56 |
//! ``` |
| 57 |
|
| 58 |
#![no_std] |
| 59 |
#![cfg_attr( |
| 60 |
all(feature = "unstable_const", not(stable_const)), |
| 61 |
feature(const_ptr_offset_from) |
| 62 |
)] |
| 63 |
#![cfg_attr(feature = "unstable_const", feature(const_refs_to_cell))] |
| 64 |
#![cfg_attr(feature = "unstable_offset_of", feature(allow_internal_unstable))] |
| 65 |
|
| 66 |
#[macro_use] |
| 67 |
#[cfg(doctests)] |
| 68 |
#[cfg(doctest)] |
| 69 |
extern crate doc_comment; |
| 70 |
#[cfg(doctests)] |
| 71 |
#[cfg(doctest)] |
| 72 |
doctest!("../README.md"); |
| 73 |
|
| 74 |
/// Hidden module for things the macros need to access. |
| 75 |
#[doc(hidden)] |
| 76 |
pub mod __priv { |
| 77 |
#[doc(hidden)] |
| 78 |
pub use core::mem; |
| 79 |
#[doc(hidden)] |
| 80 |
pub use core::ptr; |
| 81 |
|
| 82 |
/// Use type inference to obtain the size of the pointee (without actually using the pointer). |
| 83 |
#[doc(hidden)] |
| 84 |
pub fn size_of_pointee<T>(_ptr: *const T) -> usize { |
| 85 |
mem::size_of::<T>() |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[macro_use] |
| 90 |
mod raw_field; |
| 91 |
#[macro_use] |
| 92 |
mod offset_of; |
| 93 |
#[macro_use] |
| 94 |
mod span_of; |