160636f74Sopenharmony_ci// Copyright (c) 2017 Gilad Naaman
260636f74Sopenharmony_ci//
360636f74Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a copy
460636f74Sopenharmony_ci// of this software and associated documentation files (the "Software"), to deal
560636f74Sopenharmony_ci// in the Software without restriction, including without limitation the rights
660636f74Sopenharmony_ci// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
760636f74Sopenharmony_ci// copies of the Software, and to permit persons to whom the Software is
860636f74Sopenharmony_ci// furnished to do so, subject to the following conditions:
960636f74Sopenharmony_ci//
1060636f74Sopenharmony_ci// The above copyright notice and this permission notice shall be included in all
1160636f74Sopenharmony_ci// copies or substantial portions of the Software.
1260636f74Sopenharmony_ci//
1360636f74Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1460636f74Sopenharmony_ci// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1560636f74Sopenharmony_ci// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1660636f74Sopenharmony_ci// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1760636f74Sopenharmony_ci// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1860636f74Sopenharmony_ci// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1960636f74Sopenharmony_ci// SOFTWARE.
2060636f74Sopenharmony_ci
2160636f74Sopenharmony_ci//! A crate used for calculating offsets of struct members and their spans.
2260636f74Sopenharmony_ci//!
2360636f74Sopenharmony_ci//! This functionality currently can not be used in compile time code such as `const` or `const fn` definitions.
2460636f74Sopenharmony_ci//!
2560636f74Sopenharmony_ci//! ## Examples
2660636f74Sopenharmony_ci//! ```
2760636f74Sopenharmony_ci//! use memoffset::{offset_of, span_of};
2860636f74Sopenharmony_ci//!
2960636f74Sopenharmony_ci//! #[repr(C, packed)]
3060636f74Sopenharmony_ci//! struct HelpMeIAmTrappedInAStructFactory {
3160636f74Sopenharmony_ci//!     help_me_before_they_: [u8; 15],
3260636f74Sopenharmony_ci//!     a: u32
3360636f74Sopenharmony_ci//! }
3460636f74Sopenharmony_ci//!
3560636f74Sopenharmony_ci//! fn main() {
3660636f74Sopenharmony_ci//!     assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15);
3760636f74Sopenharmony_ci//!     assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19);
3860636f74Sopenharmony_ci//!     assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15);
3960636f74Sopenharmony_ci//! }
4060636f74Sopenharmony_ci//! ```
4160636f74Sopenharmony_ci//!
4260636f74Sopenharmony_ci//! This functionality can be useful, for example, for checksum calculations:
4360636f74Sopenharmony_ci//!
4460636f74Sopenharmony_ci//! ```ignore
4560636f74Sopenharmony_ci//! #[repr(C, packed)]
4660636f74Sopenharmony_ci//! struct Message {
4760636f74Sopenharmony_ci//!     header: MessageHeader,
4860636f74Sopenharmony_ci//!     fragment_index: u32,
4960636f74Sopenharmony_ci//!     fragment_count: u32,
5060636f74Sopenharmony_ci//!     payload: [u8; 1024],
5160636f74Sopenharmony_ci//!     checksum: u16
5260636f74Sopenharmony_ci//! }
5360636f74Sopenharmony_ci//!
5460636f74Sopenharmony_ci//! let checksum_range = &raw[span_of!(Message, header..checksum)];
5560636f74Sopenharmony_ci//! let checksum = crc16(checksum_range);
5660636f74Sopenharmony_ci//! ```
5760636f74Sopenharmony_ci
5860636f74Sopenharmony_ci#![no_std]
5960636f74Sopenharmony_ci#![cfg_attr(
6060636f74Sopenharmony_ci    all(feature = "unstable_const", not(stable_const)),
6160636f74Sopenharmony_ci    feature(const_ptr_offset_from)
6260636f74Sopenharmony_ci)]
6360636f74Sopenharmony_ci#![cfg_attr(feature = "unstable_const", feature(const_refs_to_cell))]
6460636f74Sopenharmony_ci
6560636f74Sopenharmony_ci#[macro_use]
6660636f74Sopenharmony_ci#[cfg(doctests)]
6760636f74Sopenharmony_ci#[cfg(doctest)]
6860636f74Sopenharmony_ciextern crate doc_comment;
6960636f74Sopenharmony_ci#[cfg(doctests)]
7060636f74Sopenharmony_ci#[cfg(doctest)]
7160636f74Sopenharmony_cidoctest!("../README.md");
7260636f74Sopenharmony_ci
7360636f74Sopenharmony_ci/// Hidden module for things the macros need to access.
7460636f74Sopenharmony_ci#[doc(hidden)]
7560636f74Sopenharmony_cipub mod __priv {
7660636f74Sopenharmony_ci    #[doc(hidden)]
7760636f74Sopenharmony_ci    pub use core::mem;
7860636f74Sopenharmony_ci    #[doc(hidden)]
7960636f74Sopenharmony_ci    pub use core::ptr;
8060636f74Sopenharmony_ci
8160636f74Sopenharmony_ci    /// Use type inference to obtain the size of the pointee (without actually using the pointer).
8260636f74Sopenharmony_ci    #[doc(hidden)]
8360636f74Sopenharmony_ci    pub fn size_of_pointee<T>(_ptr: *const T) -> usize {
8460636f74Sopenharmony_ci        mem::size_of::<T>()
8560636f74Sopenharmony_ci    }
8660636f74Sopenharmony_ci}
8760636f74Sopenharmony_ci
8860636f74Sopenharmony_ci#[macro_use]
8960636f74Sopenharmony_cimod raw_field;
9060636f74Sopenharmony_ci#[macro_use]
9160636f74Sopenharmony_cimod offset_of;
9260636f74Sopenharmony_ci#[macro_use]
9360636f74Sopenharmony_cimod span_of;
94