### Compile-fail example for _memoffset__field_check Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Doc example showing that _memoffset__field_check! prevents compilation when the field is accessed through a Deref impl. The example is marked compile_fail. ```rust use memoffset::_memoffset__field_check; struct Foo { foo: i32, } type BoxedFoo = Box; _memoffset__field_check!(BoxedFoo, foo); ``` -------------------------------- ### Compile-fail example for _memoffset__field_check_tuple Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Doc example showing that _memoffset__field_check_tuple! prevents compilation when the type is not a tuple. The example is marked compile_fail. ```rust use memoffset::_memoffset__field_check_tuple; _memoffset__field_check_tuple!(i32, 0); ``` -------------------------------- ### Compile-fail example for _memoffset__field_check_union Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Doc example showing that _memoffset__field_check_union! prevents compilation when the field is accessed through a Deref impl. The example is marked compile_fail. ```rust use memoffset::_memoffset__field_check_union; union Foo { variant_a: i32, } type BoxedFoo = Box; _memoffset__field_check_union!(BoxedFoo, variant_a); ``` -------------------------------- ### Using offset_of! and span_of! macros Source: https://docs.rs/memoffset/latest/src/memoffset/lib.rs.html Shows how to use `offset_of!` and `span_of!` to get the offset and span of struct fields. The struct must be `#[repr(C, packed)]` for the offsets to be deterministic. The example asserts the offset of field `a` is 15 and its span is 15..19, and the span from `help_me_before_they_` to `a` is 0..15. ```rust use memoffset::{offset_of, span_of}; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15); assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15); ``` -------------------------------- ### Public offset_of! macro with usage example Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines the public offset_of! macro that calculates the offset of a specified field from the start of a named struct. The example shows usage with a repr(C, packed) struct and asserts the offsets for fields a and b. Note that Rust's ABI is unstable and the offset value may change between compilations. ```rust /// Calculates the offset of the specified field from the start of the named struct. /// /// ## Examples /// ``` /// use memoffset::offset_of; /// /// #[repr(C, packed)] /// struct Foo { /// a: u32, /// b: u64, /// c: [u8; 5] /// } /// /// assert_eq!(offset_of!(Foo, a), 0); /// assert_eq!(offset_of!(Foo, b), 4); /// ``` /// /// ## Notes /// Rust's ABI is unstable, and [type layout can be changed with each /// compilation](https://doc.rust-lang.org/reference/type-layout.html). /// /// Using `offset_of!` with a `repr(Rust)` struct will return the correct offset of the /// specified `field` for a particular compilation, but the exact value may change /// based on the compiler version, concrete struct type, time of day, or rustc's mood. /// /// As a result, the value should not be retained and used between different compilations. #[macro_export(local_inner_macros)] macro_rules! offset_of { ``` -------------------------------- ### offset_of usage example Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of.html Demonstrates calculating field offsets for a packed C-representation struct. ```rust use memoffset::offset_of; #[repr(C, packed)] struct Foo { a: u32, b: u64, c: [u8; 5] } assert_eq!(offset_of!(Foo, a), 0); assert_eq!(offset_of!(Foo, b), 4); ``` -------------------------------- ### offset_of_tuple usage example Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of_tuple.html Shows how to use offset_of_tuple to get the offset of a field in a tuple. The assertion checks that the offset is non-negative, noting that tuples do not have a defined layout. ```rust use memoffset::offset_of_tuple; assert!(offset_of_tuple!((u8, u32), 1) >= 0, "Tuples do not have a defined layout"); ``` -------------------------------- ### span_of! range forms Source: https://docs.rs/memoffset/latest/src/memoffset/span_of.rs.html Examples of using range syntax to calculate spans between fields or spanning multiple fields. ```rust #[repr(C)] struct Florp { a: u32, } #[repr(C)] struct Blarg { x: u64, y: [u8; 56], z: Florp, egg: [[u8; 4]; 5], } // Love me some brute force assert_eq!(0..8, span_of!(Blarg, x)); assert_eq!(64..68, span_of!(Blarg, z)); assert_eq!(68..mem::size_of::(), span_of!(Blarg, egg)); assert_eq!(8..64, span_of!(Blarg, y..z)); assert_eq!(0..64, span_of!(Blarg, x..=y)); ``` -------------------------------- ### span_of! usage examples Source: https://docs.rs/memoffset/latest/memoffset/macro.span_of.html Examples of span_of! usage with a repr(C) struct. The assertions show the resulting ranges for various forms, including open-ended, exclusive, and inclusive ranges. ```rust use memoffset::span_of; #[repr(C)] struct Florp { a: u32 } #[repr(C)] struct Blarg { x: [u32; 2], y: [u8; 56], z: Florp, egg: [[u8; 4]; 4] } assert_eq!(0..84, span_of!(Blarg, ..)); assert_eq!(0..8, span_of!(Blarg, .. y)); assert_eq!(0..64, span_of!(Blarg, ..= y)); assert_eq!(0..8, span_of!(Blarg, x)); assert_eq!(8..84, span_of!(Blarg, y ..)); assert_eq!(0..8, span_of!(Blarg, x .. y)); assert_eq!(0..64, span_of!(Blarg, x ..= y)); ``` -------------------------------- ### Calculate union member offset Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of_union.html Example demonstrating the use of offset_of_union to verify the offset of a field in a packed union. ```rust use memoffset::offset_of_union; #[repr(C, packed)] union Foo { foo32: i32, foo64: i64, } assert!(offset_of_union!(Foo, foo64) == 0); ``` -------------------------------- ### offset_of_tuple! Source: https://docs.rs/memoffset/latest/index.html Calculates the offset of the specified field from the start of the tuple. ```APIDOC ## offset_of_tuple! ### Description Calculates the offset of the specified field from the start of the tuple. ### Usage ```rust use memoffset::offset_of_tuple; let tuple = (0u8, 1u16, 2u32); let offset = offset_of_tuple!(tuple, 1); ``` ### Parameters - **TupleType** (type) - Required - The type of the tuple. - **index** (usize) - Required - The index of the field whose offset is to be calculated. ### Returns - **usize** - The offset in bytes from the start of the tuple to the specified field. ``` -------------------------------- ### offset_of Source: https://docs.rs/memoffset/latest/memoffset/index.html Calculates the offset of the specified field from the start of the named struct. ```APIDOC ## offset_of ### Description Calculates the offset of the specified field from the start of the named struct. ### Usage Use the macro with the struct type and field name. ### Example ```rust use memoffset::offset_of; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15); ``` ``` -------------------------------- ### offset_of! Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of.html Calculates the offset of the specified field from the start of the named struct. The macro takes a parent type path and a field token, and returns the byte offset as a usize. ```APIDOC ## offset_of! ### Description Calculates the offset of the specified field from the start of the named struct. ### Syntax ```rust macro_rules! offset_of { ($parent:path, $field:tt) => { ... }; } ``` ### Parameters - `$parent` (path) - Required - The path to the struct type. - `$field` (token) - Required - The name of the field whose offset is to be calculated. ### Return Value Returns the byte offset of the field from the start of the struct, as a `usize`. ### Examples ```rust use memoffset::offset_of; #[repr(C, packed)] struct Foo { a: u32, b: u64, c: [u8; 5] } assert_eq!(offset_of!(Foo, a), 0); assert_eq!(offset_of!(Foo, b), 4); ``` ### Notes Rust's ABI is unstable, and type layout can be changed with each compilation. Using `offset_of!` with a `repr(Rust)` struct will return the correct offset of the specified `field` for a particular compilation, but the exact value may change based on the compiler version, concrete struct type, time of day, or rustc's mood. As a result, the value should not be retained and used between different compilations. ``` -------------------------------- ### offset_of! Source: https://docs.rs/memoffset/latest/index.html Calculates the offset of the specified field from the start of the named struct. ```APIDOC ## offset_of! ### Description Calculates the offset of the specified field from the start of the named struct. ### Usage ```rust use memoffset::offset_of; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(offset_of!(HelpMeIAmTrappedInAStructFactory, a), 15); ``` ### Parameters - **StructType** (type) - Required - The name of the struct. - **field** (field name) - Required - The field whose offset is to be calculated. ### Returns - **usize** - The offset in bytes from the start of the struct to the specified field. ``` -------------------------------- ### offset_of_tuple Source: https://docs.rs/memoffset/latest/memoffset/index.html Calculates the offset of the specified field from the start of the tuple. ```APIDOC ## offset_of_tuple ### Description Calculates the offset of the specified field from the start of the tuple. ### Usage Use the macro with the tuple type and field index. ### Example ```rust use memoffset::offset_of_tuple; let tuple = (0u8, 1u16, 2u32); let offset = offset_of_tuple!(tuple, 1); ``` ``` -------------------------------- ### Internal macro _memoffset__offset_of_impl without stable_offset_of feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to implement the offset_of! macro when the stable_offset_of feature is not enabled. It creates a base pointer, gets the field pointer using raw_field!, and computes the offset using _memoffset_offset_from_unsafe. ```rust #[cfg(not(stable_offset_of))] #[macro_export(local_inner_macros)] #[doc(hidden)] macro_rules! _memoffset__offset_of_impl { ($parent:path, $field:tt) => {{ // Get a base pointer (non-dangling if rustc supports `MaybeUninit`). _memoffset__let_base_ptr!(base_ptr, $parent); // Get field pointer. let field_ptr = raw_field!(base_ptr, $parent, $field); // Compute offset. _memoffset_offset_from_unsafe!(field_ptr, base_ptr) }}; } ``` -------------------------------- ### offset_of_tuple! Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Calculates the offset of the specified field from the start of the tuple. Note that tuples do not have a defined layout, so the offset may be non-negative but not guaranteed to be zero. ```APIDOC ## offset_of_tuple! ### Description Calculates the offset of the specified field from the start of the tuple. ### Syntax `offset_of_tuple!($parent:ty, $field:tt)` ### Parameters - `$parent` (ty) - Required - The tuple type. - `$field` (tt) - Required - The field index (e.g., 0, 1, ...). ### Returns The byte offset of the field from the start of the tuple. ### Example ```rust use memoffset::offset_of_tuple; assert!(offset_of_tuple!((u8, u32), 1) >= 0, "Tuples do not have a defined layout"); ``` ``` -------------------------------- ### offset_of_union! Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of_union.html Calculates the offset of the specified union member from the start of the union. ```APIDOC ## offset_of_union! ### Description Calculates the offset of the specified union member from the start of the union. ### Syntax ```rust macro_rules! offset_of_union { ($parent:path, $field:tt) => { ... }; } ``` ### Parameters - **$parent** (path) - Required - The union type. - **$field** (token tree) - Required - The field name whose offset is to be calculated. ### Return Value Returns the offset in bytes from the start of the union to the specified field. ### Example ```rust use memoffset::offset_of_union; #[repr(C, packed)] union Foo { foo32: i32, foo64: i64, } assert!(offset_of_union!(Foo, foo64) == 0); ``` ### Note Due to `macro_rules!` limitations, this macro will accept structs with a single field as well as unions. This is not a stable guarantee, and future versions of this crate might fail on any use of this macro with a struct, without a semver bump. ``` -------------------------------- ### offset_of_tuple Source: https://docs.rs/memoffset/latest/memoffset Calculates the offset of a specified field from the start of a tuple. Useful for tuple structures. ```APIDOC ## offset_of_tuple! ### Description Calculates the offset of the specified field from the start of the tuple. This macro provides a way to obtain field offsets for tuples. ### Method ```rust memoffset::offset_of_tuple!((u8, u32), 1) ``` ``` -------------------------------- ### offset_of_union Source: https://docs.rs/memoffset/latest/memoffset/index.html Calculates the offset of the specified union member from the start of the union. ```APIDOC ## offset_of_union ### Description Calculates the offset of the specified union member from the start of the union. ### Usage Use the macro with the union type and member name. ### Example ```rust use memoffset::offset_of_union; union MyUnion { a: u32, b: f32, } let offset = offset_of_union!(MyUnion, b); ``` ``` -------------------------------- ### offset_of_tuple macro definition Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of_tuple.html Defines the offset_of_tuple macro that takes a tuple type and a field token, returning the offset of that field from the start of the tuple. ```rust macro_rules! offset_of_tuple { ($parent:ty, $field:tt) => { ... }; } ``` -------------------------------- ### Checksum calculation with span_of! Source: https://docs.rs/memoffset/latest/src/memoffset/lib.rs.html Illustrates using `span_of!` to compute a range for checksum calculation. The struct `Message` is `#[repr(C, packed)]` and the range from `header` to `checksum` is used to compute a CRC16 checksum. This example is marked `ignore` in the source, so it is not run as a test. ```rust #[repr(C, packed)] struct Message { header: MessageHeader, fragment_index: u32, fragment_count: u32, payload: [u8; 1024], checksum: u16 } let checksum_range = &raw[span_of!(Message, header..checksum)]; let checksum = crc16(checksum_range); ``` -------------------------------- ### offset_of_union Source: https://docs.rs/memoffset/latest/memoffset Calculates the offset of a specified union member from the start of the union. Works with union types. ```APIDOC ## offset_of_union! ### Description Calculates the offset of the specified union member from the start of the union. ### Method ```rust memoffset::offset_of_union!(MyUnion, field) ``` ``` -------------------------------- ### raw_field macro: compute const raw pointer to a struct field Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Defines the raw_field macro. It takes a base expression, a parent type path, and a field token. It first runs a field existence check to prevent deref coercion, then evaluates the base expression outside the unsafe block, and finally uses addr_of to get the field address. The base pointer must not be dangling but may point to uninitialized memory. ```rust macro_rules! raw_field { ($base:expr, $parent:path, $field:tt) => {{ _memoffset__field_check!($parent, $field); let base = $base; // evaluate $base outside the `unsafe` block // Get the field address. // Crucially, we know that this will not trigger a deref coercion because // of the field check we did above. #[allow(unused_unsafe)] // for when the macro is used in an unsafe block unsafe { _memoffset__addr_of!((*(base as *const $parent)).$field) } }}; } ``` -------------------------------- ### span_of!(Struct, member_a .. member_b) Source: https://docs.rs/memoffset/latest/memoffset/macro.span_of.html Produces a sub-slice which starts at one field and ends at another. Supports exclusive, inclusive, and open-ended ranges. ```APIDOC ## span_of!(Struct, member_a .. member_b) ### Description Produces a sub-slice which starts at one field and ends at another. Supports exclusive, inclusive, and open-ended ranges. ### Syntax span_of!(Struct, member_a .. member_b) // Exclusive span_of!(Struct, member_a ..= member_b) // Inclusive span_of!(Struct, .. end) // Open-ended range (start to end) span_of!(Struct, start ..) // Open-ended range (start to end) ### Parameters - **Struct** (type path) - Required - The struct type to inspect. - **member_a** (field identifier) - Required - The starting field. - **member_b** (field identifier) - Required - The ending field. - **end** (field identifier) - Required for open-ended ranges - The ending field. - **start** (field identifier) - Required for open-ended ranges - The starting field. ### Safety This form assumes that the first field is positioned before the second. This is only guaranteed for repr(C) structs. Usage with repr(Rust) structs may yield unexpected results. ### Example ```rust use memoffset::span_of; #[repr(C)] struct Florp { a: u32 } #[repr(C)] struct Blarg { x: [u32; 2], y: [u8; 56], z: Florp, egg: [[u8; 4]; 4] } assert_eq!(0..84, span_of!(Blarg, ..)); assert_eq!(0..8, span_of!(Blarg, .. y)); assert_eq!(0..64, span_of!(Blarg, ..= y)); assert_eq!(8..84, span_of!(Blarg, y ..)); assert_eq!(0..8, span_of!(Blarg, x .. y)); assert_eq!(0..64, span_of!(Blarg, x ..= y)); ``` ``` -------------------------------- ### Define _memoffset__addr_of macro with raw_ref_macros Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that uses addr_of! when raw_ref_macros is enabled. This is the first of two cfg variants for the same macro. ```rust /// `addr_of!`, or just ref-then-cast when that is not available. #[cfg(raw_ref_macros)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__addr_of { ($path:expr) => {{ $crate::__priv::ptr::addr_of!($path) }}; } ``` -------------------------------- ### Basic span_of! usage Source: https://docs.rs/memoffset/latest/src/memoffset/span_of.rs.html Demonstrates calculating the byte range of individual fields in a standard C-representation struct. ```rust #[repr(C)] struct Foo { a: u32, b: [u8; 2], c: i64, } assert_eq!(span_of!(Foo, a), 0..4); assert_eq!(span_of!(Foo, b), 4..6); assert_eq!(span_of!(Foo, c), 8..8 + 8); ``` -------------------------------- ### Internal macro _memoffset__let_base_ptr with maybe_uninit feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to create a local base_ptr raw pointer of the given type, avoiding undefined behavior as much as possible when the maybe_uninit feature is enabled. It uses MaybeUninit to ensure the pointer does not dangle. ```rust /// Macro to create a local `base_ptr` raw pointer of the given type, avoiding UB as /// much as is possible currently. #[cfg(maybe_uninit)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__let_base_ptr { ($name:ident, $type:ty) => { // No UB here, and the pointer does not dangle, either. // But we have to make sure that `uninit` lives long enough, // so it has to be in the same scope as `$name`. That's why // `let_base_ptr` declares a variable (several, actually) // instead of returning one. let uninit = $crate::__priv::mem::MaybeUninit::<$type>::uninit(); let $name: *const $type = uninit.as_ptr(); }; } ``` -------------------------------- ### Define _memoffset__field_check macro with allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks field existence via a let pattern, with clippy allow attribute. This is the allow_clippy variant. ```rust #[cfg(allow_clippy)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check { ($type:path, $field:tt) => { // Make sure the field actually exists. This line ensures that a // compile-time error is generated if $field is accessed through a // Deref impl. #[allow(clippy::unneeded_field_pattern)] let $type { $field: _, .. }; }; } ``` -------------------------------- ### Calculate constant offsets with interior mutability Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Demonstrates constant offset calculation for structs containing types with interior mutability. ```rust #[cfg(stable_offset_of)] #[test] fn const_offset_interior_mutable() { #[repr(C)] struct Foo { a: u32, b: core::cell::Cell, } assert_eq!([0; offset_of!(Foo, b)].len(), 4); } ``` -------------------------------- ### Internal macro _memoffset__let_base_ptr without maybe_uninit feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to create a base pointer when the maybe_uninit feature is not enabled. It uses align_of to create a pointer, but notes that dereferencing it later is undefined behavior because the pointer is dangling. ```rust #[cfg(not(maybe_uninit))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__let_base_ptr { ($name:ident, $type:ty) => { // No UB right here, but we will later dereference this pointer to // offset into a field, and that is UB because the pointer is dangling. let $name = $crate::__priv::mem::align_of::<$type>() as *const $type; }; } ``` -------------------------------- ### Access raw union fields Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Demonstrates using raw_field_union! to calculate the address of union fields. ```rust #[test] fn test_raw_field_union() { #[repr(C)] union Foo { a: u32, b: [u8; 2], c: i64, } let f = Foo { a: 0 }; let f_ptr = &f as *const _; assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, a) as usize); assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, b) as usize); assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize); } ``` -------------------------------- ### Define _memoffset__field_check_tuple macro with allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks the type argument is a tuple, with clippy allow attribute. This is the allow_clippy variant. ```rust #[cfg(allow_clippy)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check_tuple { ($type:ty, $field:tt) => { // Make sure the type argument is a tuple #[allow(clippy::unneeded_wildcard_pattern)] let (_, ..): $type; }; } ``` -------------------------------- ### Define _memoffset__field_check macro without allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks field existence via a let pattern, without clippy allow attribute. This is the non-allow_clippy variant. ```rust #[cfg(not(allow_clippy))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check { ($type:path, $field:tt) => { // Make sure the field actually exists. This line ensures that a // compile-time error is generated if $field is accessed through a // Deref impl. let $type { $field: _, .. }; }; } ``` -------------------------------- ### Calculate constant offsets Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Shows that offset_of! can be used in constant contexts when supported by the compiler. ```rust #[cfg(any(stable_offset_of, stable_const))] #[test] fn const_offset() { #[repr(C)] struct Foo { a: u32, b: [u8; 2], c: i64, } assert_eq!([0; offset_of!(Foo, b)].len(), 4); } ``` -------------------------------- ### Define _memoffset__addr_of macro without raw_ref_macros Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Fallback macro that uses reference-then-cast, noting it is UB without addr_of!. This is the second cfg variant. ```rust #[cfg(not(raw_ref_macros))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__addr_of { ($path:expr) => {{ // This is UB because we create an intermediate reference to uninitialized memory. // Nothing we can do about that without `addr_of!` though. &$path as *const _ }}; } ``` -------------------------------- ### Calculate offset inside generic method Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Shows how to use offset_of! within a generic function context. ```rust #[test] fn inside_generic_method() { struct Pair(T, U); fn foo(_: Pair) -> usize { offset_of!(Pair, 1) } assert_eq!(foo(Pair(0, 0)), 4); } ``` -------------------------------- ### Define _memoffset__field_check_union macro with allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks union field existence via an unsafe pattern, with clippy allow attributes. This is the allow_clippy variant. ```rust #[cfg(allow_clippy)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check_union { ($type:path, $field:tt) => { // Make sure the field actually exists. This line ensures that a // compile-time error is generated if $field is accessed through a // Deref impl. #[allow(clippy::unneeded_wildcard_pattern)] // rustc1.19 requires unsafe here for the pattern; not needed in newer versions #[allow(unused_unsafe)] unsafe { let $type { $field: _ }; } }; } ``` -------------------------------- ### span_of! range forms Source: https://docs.rs/memoffset/latest/memoffset/macro.span_of.html Second form of span_of! returns a sub-slice that starts at one field and ends at another. Supports exclusive (..), inclusive (..=), and open-ended ranges. The inter-field form assumes the first field is positioned before the second; this is only guaranteed for repr(C) structs. Usage with repr(Rust) structs may yield unexpected results, such as downward-going ranges, spans that include unexpected fields, empty spans, or spans that include unexpected padding bytes. ```rust // Exclusive span_of!(Struct, member_a .. member_b) // Inclusive span_of!(Struct, member_a ..= member_b) // Open-ended ranges span_of!(Struct, .. end) span_of!(Struct, start ..) ``` -------------------------------- ### offset_of macro definition Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of.html The signature of the offset_of macro. ```rust macro_rules! offset_of { ($parent:path, $field:tt) => { ... }; } ``` -------------------------------- ### Access raw struct fields Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Demonstrates using raw_field! to calculate the address of a struct field from a raw pointer. ```rust #[test] fn test_raw_field() { #[repr(C)] struct Foo { a: u32, b: [u8; 2], c: i64, } let f: Foo = Foo { a: 0, b: [0, 0], c: 0, }; let f_ptr = &f as *const _; assert_eq!(f_ptr as usize + 0, raw_field!(f_ptr, Foo, a) as usize); assert_eq!(f_ptr as usize + 4, raw_field!(f_ptr, Foo, b) as usize); assert_eq!(f_ptr as usize + 8, raw_field!(f_ptr, Foo, c) as usize); } ``` -------------------------------- ### Define _memoffset__field_check_tuple macro without allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks the type argument is a tuple, without clippy allow attribute. This is the non-allow_clippy variant. ```rust #[cfg(not(allow_clippy))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check_tuple { ($type:ty, $field:tt) => { // Make sure the type argument is a tuple let (_, ..): $type; }; } ``` -------------------------------- ### span_of Source: https://docs.rs/memoffset/latest/memoffset/index.html Produces a range instance representing the sub-slice containing the specified member. ```APIDOC ## span_of ### Description Produces a range instance representing the sub-slice containing the specified member. ### Usage Use the macro with the struct type and field name or a range of fields. ### Example ```rust use memoffset::span_of; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15); ``` ``` -------------------------------- ### Define _memoffset__field_check_union macro without allow_clippy Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Internal macro that checks union field existence via an unsafe pattern, without clippy allow attributes. This is the non-allow_clippy variant. ```rust #[cfg(not(allow_clippy))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__field_check_union { ($type:path, $field:tt) => { // Make sure the field actually exists. This line ensures that a // compile-time error is generated if $field is accessed through a // Deref impl. #[allow(unused_unsafe)] unsafe { let $type { $field: _ }; } }; } ``` -------------------------------- ### span_of Source: https://docs.rs/memoffset/latest/memoffset Produces a range instance representing the sub-slice containing the specified member. It can also produce a range between two fields. ```APIDOC ## span_of! ### Description Produces a range instance representing the sub-slice containing the specified member. It can be used to obtain byte ranges for a single field or a range of fields. ### Method ```rust use memoffset::span_of; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_..a), 0..15); ``` ``` -------------------------------- ### span_of! Source: https://docs.rs/memoffset/latest/index.html Produces a range instance representing the sub-slice containing the specified member. ```APIDOC ## span_of! ### Description Produces a range instance representing the sub-slice containing the specified member. ### Usage ```rust use memoffset::span_of; #[repr(C, packed)] struct HelpMeIAmTrappedInAStructFactory { help_me_before_they_: [u8; 15], a: u32 } assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, a), 15..19); assert_eq!(span_of!(HelpMeIAmTrappedInAStructFactory, help_me_before_they_ .. a), 0..15); ``` ### Parameters - **StructType** (type) - Required - The name of the struct. - **field** (field name) - Required - The field or range of fields whose span is to be produced. ### Returns - **Range** - A range instance representing the sub-slice containing the specified member. ``` -------------------------------- ### offset_of_tuple! Source: https://docs.rs/memoffset/latest/memoffset/macro.offset_of_tuple.html Macro to calculate the offset of a field within a tuple. ```APIDOC ## Macro `offset_of_tuple!` ### Description Calculates the offset of the specified field from the start of the tuple. ### Macro Definition ```rust macro_rules! offset_of_tuple { ($parent:ty, $field:tt) => { ... }; } ``` ### Parameters - **$parent** (type) - Required - The tuple type whose field offset is to be calculated. - **$field** (token) - Required - The index of the field within the tuple. ### Example ```rust use memoffset::offset_of_tuple; assert!(offset_of_tuple!((u8, u32), 1) >= 0, "Tuples do not have a defined layout"); ``` ``` -------------------------------- ### Calculate offsets in constant functions Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Shows the use of offset_of! inside a const function. ```rust #[cfg(any(stable_offset_of, stable_const))] #[test] fn const_fn_offset() { const fn test_fn() -> usize { #[repr(C)] struct Foo { a: u32, b: [u8; 2], c: i64, } offset_of!(Foo, b) } assert_eq!([0; test_fn()].len(), 4); } ``` -------------------------------- ### raw_field! Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Macro to compute a const raw pointer to a field of a given base pointer and parent type. ```APIDOC ## raw_field! ### Description Computes a const raw pointer to the given field of the given base pointer to the given parent type. The `base` pointer *must not* be dangling, but it *may* point to uninitialized memory. ### Syntax ```rust raw_field!($base:expr, $parent:path, $field:tt) ``` ### Parameters - **$base** (expr) - Required - The base pointer expression. Must not be dangling, but may point to uninitialized memory. - **$parent** (path) - Required - The parent type path. - **$field** (tt) - Required - The field token tree. ### Return Value A const raw pointer to the given field of the given base pointer. ### Notes - The macro performs a compile-time field existence check. - The field address is computed without triggering deref coercion. - The macro is exported with `#[macro_export(local_inner_macros)]`. ``` -------------------------------- ### Internal macro _memoffset__offset_of_impl with stable_offset_of feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to implement the offset_of! macro when the stable_offset_of feature is enabled. It delegates to the standard library's mem::offset_of! macro. ```rust #[cfg(stable_offset_of)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset__offset_of_impl { ($parent:path, $field:tt) => {{ $crate::__priv::mem::offset_of!($parent, $field) }}; } ``` -------------------------------- ### Calculate struct field offset Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Demonstrates using offset_of! to find the byte offset of a field within a struct. ```rust pub struct Foo { pub x: u32, } } assert_eq!(offset_of!(sub::Foo, x), 0); ``` -------------------------------- ### raw_field_tuple macro definition Source: https://docs.rs/memoffset/latest/memoffset/macro.raw_field_tuple.html The signature of the raw_field_tuple macro. ```rust macro_rules! raw_field_tuple { ($base:expr, $parent:ty, $field:tt) => { ... }; } ``` -------------------------------- ### span_of! with packed structs Source: https://docs.rs/memoffset/latest/src/memoffset/span_of.rs.html Shows how span_of! behaves with structs using #[repr(C, packed)], where field alignment is minimized. ```rust #[repr(C, packed)] struct Foo { a: u32, b: [u8; 2], c: i64, } assert_eq!(span_of!(Foo, a), 0..4); assert_eq!(span_of!(Foo, b), 4..6); assert_eq!(span_of!(Foo, c), 6..6 + 8); ``` -------------------------------- ### span_of! complex range expressions Source: https://docs.rs/memoffset/latest/src/memoffset/span_of.rs.html Demonstrates advanced range usage including open-ended ranges and inclusive/exclusive boundaries. ```rust #[repr(C)] struct Member { foo: u32, } #[repr(C)] struct Test { x: u64, y: [u8; 56], z: Member, egg: [[u8; 4]; 4], } assert_eq!(span_of!(Test, ..x), 0..0); assert_eq!(span_of!(Test, ..=x), 0..8); assert_eq!(span_of!(Test, ..y), 0..8); assert_eq!(span_of!(Test, ..=y), 0..64); assert_eq!(span_of!(Test, ..z), 0..64); assert_eq!(span_of!(Test, ..=z), 0..68); assert_eq!(span_of!(Test, ..egg), 0..68); assert_eq!(span_of!(Test, ..=egg), 0..84); assert_eq!(span_of!(Test, ..), 0..mem::size_of::()); assert_eq!( span_of!(Test, x..), offset_of!(Test, x)..mem::size_of::() ); assert_eq!( span_of!(Test, y..), offset_of!(Test, y)..mem::size_of::() ); assert_eq!( span_of!(Test, z..), offset_of!(Test, z)..mem::size_of::() ); assert_eq!( span_of!(Test, egg..), offset_of!(Test, egg)..mem::size_of::() ); assert_eq!( span_of!(Test, x..y), offset_of!(Test, x)..offset_of!(Test, y) ); assert_eq!( span_of!(Test, x..=y), offset_of!(Test, x)..offset_of!(Test, y) + mem::size_of::<[u8; 56]>() ); ``` -------------------------------- ### Internal macro _memoffset_offset_from_unsafe without stable_const feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to compute the distance between two pointers when the stable_const feature is not enabled. It computes the offset by subtracting the base pointer address from the field pointer address. ```rust #[cfg(not(stable_const))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset_offset_from_unsafe { ($field:expr, $base:expr) => { // Compute offset. ($field as usize) - ($base as usize) }; } ``` -------------------------------- ### raw_field_union! Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Macro to compute a const raw pointer to a field of a given base pointer and parent type, with a Deref-coercion check that supports unions. ```APIDOC ## raw_field_union! ### Description Computes a const raw pointer to the given field of the given base pointer to the given parent tuple type. The `base` pointer *must not* be dangling, but it *may* point to uninitialized memory. This macro is the same as `raw_field`, except for a different Deref-coercion check that supports unions. ### Syntax ```rust raw_field_union!($base:expr, $parent:path, $field:tt) ``` ### Parameters - **$base** (expr) - Required - The base pointer expression. Must not be dangling, but may point to uninitialized memory. - **$parent** (path) - Required - The parent type path. - **$field** (tt) - Required - The field token tree. ### Return Value A const raw pointer to the given field of the given base pointer. ### Notes - The macro performs a compile-time field existence check. - The field address is computed without triggering deref coercion. - The macro is exported with `#[macro_export(local_inner_macros)]`. - Due to `macro_rules!` limitations, this check will accept structs with a single field as well as unions. This is not a stable guarantee, and future versions of this crate might fail on any use of this macro with a struct, without a semver bump. ``` -------------------------------- ### raw_field_tuple Source: https://docs.rs/memoffset/latest/memoffset/macro.raw_field_tuple.html Documentation for the raw_field_tuple macro, which computes a const raw pointer to a field of a tuple type. ```APIDOC ## raw_field_tuple ### Description Computes a const raw pointer to the given field of the given base pointer to the given parent tuple type. The `base` pointer must not be dangling, but it may point to uninitialized memory. ### Syntax ```rust macro_rules! raw_field_tuple { ($base:expr, $parent:ty, $field:tt) => { ... }; } ``` ### Parameters - **$base** (expr) - Required - The base pointer expression. Must not be dangling, but may point to uninitialized memory. - **$parent** (ty) - Required - The parent tuple type. - **$field** (tt) - Required - The field token tree identifying the field within the tuple. ### Returns A const raw pointer to the specified field of the parent tuple type. ### Usage This macro is used to obtain a raw pointer to a field of a tuple type from a base pointer, without requiring the memory to be initialized. ``` -------------------------------- ### raw_field_tuple! Source: https://docs.rs/memoffset/latest/src/memoffset/raw_field.rs.html Macro to compute a const raw pointer to a field of a given base pointer and parent tuple type. ```APIDOC ## raw_field_tuple! ### Description Computes a const raw pointer to the given field of the given base pointer to the given parent tuple type. The `base` pointer *must not* be dangling, but it *may* point to uninitialized memory. ### Syntax ```rust raw_field_tuple!($base:expr, $parent:ty, $field:tt) ``` ### Parameters - **$base** (expr) - Required - The base pointer expression. Must not be dangling, but may point to uninitialized memory. - **$parent** (ty) - Required - The parent tuple type. - **$field** (tt) - Required - The field token tree. ### Return Value A const raw pointer to the given field of the given base pointer. ### Notes - The macro performs a compile-time field existence check. - The field address is computed without triggering deref coercion. - The macro is exported with `#[macro_export(local_inner_macros)]`. ``` -------------------------------- ### Internal macro _memoffset_offset_from_unsafe with stable_const feature Source: https://docs.rs/memoffset/latest/src/memoffset/offset_of.rs.html Defines a macro to compute the distance between two pointers when the stable_const feature is enabled. It evaluates the field and base expressions outside the unsafe block and uses offset_from for const-compatibility. ```rust /// Macro to compute the distance between two pointers. #[cfg(stable_const)] #[macro_export] #[doc(hidden)] macro_rules! _memoffset_offset_from_unsafe { ($field:expr, $base:expr) => {{ let field = $field; // evaluate $field outside the `unsafe` block let base = $base; // evaluate $base outside the `unsafe` block // Compute offset, with unstable `offset_from` for const-compatibility. // (Requires the pointers to not dangle, but we already need that for `raw_field!` anyway.) unsafe { (field as *const u8).offset_from(base as *const u8) as usize } }}; } ```