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

#101 in Rust patterns

Download history 58274/week @ 2024-01-06 55452/week @ 2024-01-13 59749/week @ 2024-01-20 61428/week @ 2024-01-27 69715/week @ 2024-02-03 69557/week @ 2024-02-10 66627/week @ 2024-02-17 68541/week @ 2024-02-24 75377/week @ 2024-03-02 75184/week @ 2024-03-09 78263/week @ 2024-03-16 80014/week @ 2024-03-23 76590/week @ 2024-03-30 77314/week @ 2024-04-06 79487/week @ 2024-04-13 71269/week @ 2024-04-20

316,791 downloads per month
Used in 490 crates (16 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

~45KB