Skip to main content

Crate const_field_offset

Crate const_field_offset 

Source
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§

FieldOffset
Represents a pointer to a field of type U within the type T

Enums§

AllowPin
Type that can be used in the PinFlag parameter of FieldOffset to specify that this projection is valid on Pin types. See documentation of FieldOffset::new_from_offset_pinned
NotPinned
Type that can be used in the PinFlag parameter of FieldOffset to specify that this projection is not valid on Pin types.

Traits§

PinnedDrop
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§

FieldOffsets
The macro FieldOffsets adds a FIELD_OFFSETS associated 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 type const_field_offset::FieldOffset