Macro repr_offset::PUB_OFF

source ·
macro_rules! PUB_OFF {
    (
        $(:: $(@$leading:tt@)? )? $first:ident $(:: $trailing:ident)* ;
        $($fields:tt).+
    ) => { ... };
    ($type:ty; $($fields:tt).+ ) => { ... };
}
Expand description

Gets the FieldOffset for the passed in type and (possibly nested) public field.

This is the same as the OFF macro, except that this can’t access private fields, and it allows accessing fields from type parameters in generic functions.

§Examples

§Named Type

use repr_offset::{
    for_examples::ReprC,
    PUB_OFF,
    FieldOffset, ROExtAcc,
};

let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};

// Passing the type as a path
assert_eq!(PUB_OFF!(ReprC; a).get(&this), &this.a);

// Passing the type as a type
assert_eq!(PUB_OFF!(ReprC<_, _, _, _>; b).get(&this), &this.b);

// Passing the type as a path
assert_eq!(this.f_get(PUB_OFF!(ReprC; c)), &this.c);

// Passing the type as a type
assert_eq!(this.f_get(PUB_OFF!(ReprC<_, _, _, _>; d)), &this.d);

§Accessing fields from type parameters

use repr_offset::{
    for_examples::ReprC,
    tstr::TS,
    PUB_OFF,
    FieldOffset, GetPubFieldOffset, ROExtOps,
};

let this = ReprC {a: 3u8, b: 5u8, c: 8u8, d: 13u8};

assertions(this);

fn assertions<T, A>(this: T)
where
    T: GetPubFieldOffset<TS!(a), Type = u8, Alignment = A>,
    T: GetPubFieldOffset<TS!(b), Type = u8, Alignment = A>,
    T: GetPubFieldOffset<TS!(c), Type = u8, Alignment = A>,
    T: GetPubFieldOffset<TS!(d), Type = u8, Alignment = A>,
    T: ROExtOps<A>,
{
    assert_eq!(this.f_get_copy(PUB_OFF!(T; a)), 3);
    assert_eq!(this.f_get_copy(PUB_OFF!(T; b)), 5);
    assert_eq!(this.f_get_copy(PUB_OFF!(T; c)), 8);
    assert_eq!(this.f_get_copy(PUB_OFF!(T; d)), 13);
}