Expand description
This crate provides the FieldOffsets derive macro to obtain constant
FieldOffsets for fields of a #[repr(C)] struct.
The FieldOffset type is re-exported from the field-offset crate.
§Usage
use const_field_offset::{FieldOffsets, FieldOffset};
#[derive(FieldOffsets)]
#[repr(C)]
struct Foo { a: u32, b: f64 }
let foo_b: FieldOffset<Foo, f64> = Foo::FIELD_OFFSETS.b();
assert_eq!(*foo_b.apply(&Foo { a: 1, b: 42.0 }), 42.0);The FIELD_OFFSETS constant is a zero-sized type with a const fn method
per field. Each method returns the corresponding FieldOffset.
For pin-projecting offsets, use #[pin]:
use const_field_offset::{FieldOffsets, FieldOffset, AllowPin};
#[derive(FieldOffsets)]
#[repr(C)]
#[pin]
struct Foo { a: u32, b: f64 }
let foo_b: FieldOffset<Foo, f64, AllowPin> = Foo::FIELD_OFFSETS.b();
let pinned = Box::pin(Foo { a: 1, b: 42.0 });
assert_eq!(*foo_b.apply_pin(pinned.as_ref()), 42.0);The #[pin] attribute enforces that the struct is !Unpin and does not
implement Drop (use #[pin_drop] with PinnedDrop instead).
Structs§
- Field
Offset - Represents a pointer to a field of type
Uwithin the typeT
Enums§
- Allow
Pin - Type that can be used in the
PinFlagparameter ofFieldOffsetto specify that this projection is valid on Pin types. See documentation ofFieldOffset::new_from_offset_pinned - NotPinned
- Type that can be used in the
PinFlagparameter ofFieldOffsetto specify that this projection is not valid on Pin types.
Traits§
- Pinned
Drop - This trait needs to be implemented if you use the
#[pin_drop]attribute. It enables you to implement Drop for your type safely.
Derive Macros§
- Field
Offsets - The macro FieldOffsets adds a
FIELD_OFFSETSassociated const to the struct. That is an object which has fields with the same name as the fields of the original struct, each field is of typeconst_field_offset::FieldOffset