#offset #field #offset-of #safe

field-offset

Safe pointer-to-member implementation

8 releases

Uses old Rust 2015

0.3.4 May 12, 2021
0.3.3 Feb 10, 2021
0.3.2 Aug 24, 2020
0.3.1 May 22, 2020
0.1.1 May 2, 2016
Download history 5946/week @ 2021-07-04 6760/week @ 2021-07-11 6502/week @ 2021-07-18 7127/week @ 2021-07-25 7455/week @ 2021-08-01 7776/week @ 2021-08-08 7923/week @ 2021-08-15 9451/week @ 2021-08-22 9897/week @ 2021-08-29 8983/week @ 2021-09-05 9947/week @ 2021-09-12 12760/week @ 2021-09-19 14193/week @ 2021-09-26 13580/week @ 2021-10-03 13439/week @ 2021-10-10 13144/week @ 2021-10-17

21,542 downloads per month
Used in 98 crates (9 directly)

MIT/Apache

20KB
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

~130KB