10 releases

Uses old Rust 2015

0.3.6 Jun 2, 2023
0.3.5 Mar 4, 2023
0.3.4 May 12, 2021
0.3.3 Feb 10, 2021
0.1.1 May 2, 2016

#108 in Rust patterns

Download history 87673/week @ 2024-07-22 87341/week @ 2024-07-29 87855/week @ 2024-08-05 89587/week @ 2024-08-12 89547/week @ 2024-08-19 93140/week @ 2024-08-26 97660/week @ 2024-09-02 87723/week @ 2024-09-09 86756/week @ 2024-09-16 94862/week @ 2024-09-23 98090/week @ 2024-09-30 102209/week @ 2024-10-07 102084/week @ 2024-10-14 100354/week @ 2024-10-21 105163/week @ 2024-10-28 107906/week @ 2024-11-04

426,416 downloads per month
Used in 615 crates (17 directly)

MIT/Apache

21KB
295 lines

field-offset: safe pointer-to-member functionality

This crate implements an offset_of!(...) macro which safely encapsulates a pointer-to-member.

Example:

struct Foo {
    x: u32,
    y: f64
}

let foo_y = offset_of!(Foo => y);

let mut a = Foo { x: 1, y: 2.0 };

*foo_y.apply_mut(&mut a) = 3.0;

assert!(a.y == 3.0);

The macro returns an instance of FieldOffset<T, U>, which represents a pointer to a field of type U within a containing type, T.

The FieldOffset type implements Add. Applying the resulting field offset is equivalent to applying the first field offset, then applying the second field offset.

The macro also supports accessing nested fields:

let bar_foo_y = offset_of!(Bar => foo: Foo => y);

Dependencies

~46KB