[][src]Macro structural::field_path_aliases_module

macro_rules! field_path_aliases_module {
    (
        $(#[$attr:meta])*
        $vis:vis mod $mod_name:ident{
            $($mod_contents:tt)*
        }
    ) => { ... };
}

Declares a module with aliases for field paths,used to access fields.

Every one of these aliases are types and constants of the same name.

Example

use structural::{field_path_aliases_module,GetField,GetFieldExt};

field_path_aliases_module!{
    pub mod names{
        // Equivalent to _a=_a
        _a,
        // Equivalent to _b=_b
        _b,
        // Equivalent to _0=_0
        _0,
        // Equivalent to c=c
        c,
        zero=0,
        one=1,
        two=2,
        e=10,
        g=abcd,

        // Used to access the `0`,`1`,and `2` fields
        // with the fields or fields_mut method.
        FirstThree=(0,1,2),
        h=(a,b,c),
        i=(0,3,5),

        j=(p), // The identifier can also be parenthesised

    }
}


fn assert_fields<T>(this:&T)
where
    T:GetField<names::zero,Ty=i32>+
        GetField<names::one,Ty=i32>+
        GetField<names::two,Ty=i32>
{
    assert_eq!( this.field_(names::zero), &2 );
    assert_eq!( this.field_(names::one), &3 );
    assert_eq!( this.field_(names::two), &5 );
    assert_eq!( this.fields(names::FirstThree), (&2,&3,&5) );
}

fn main(){
    assert_fields(&(2,3,5));
    assert_fields(&(2,3,5,8));
    assert_fields(&(2,3,5,8,13));
    assert_fields(&(2,3,5,8,13,21));
}

Example

This demonstrates defining and using aliases for nested fields.

use structural::{field_path_aliases_module,make_struct,structural_alias,GetFieldExt};

field_path_aliases_module!{
    pub mod names{
        a=0.0, // This is for accessing the `.0.0` nested field.
        b=0.1, // This is for accessing the `.0.1` nested field.
        c=foo.boo, // This is for accessing the `.foo.bar` nested field.
        d=foo.bar.baz, // This is for accessing the `.foo.bar.baz` nested field.
    }
}

structural_alias!{
    trait Foo<T>{
        foo:T,
    }

    trait Bar<T>{
        boo:u32,
        bar:T,
    }

    trait Baz<T>{
        baz:T,
    }
}

fn assert_nested<A,B,C>(this:&A)
where
    A:Foo<B>,
    B:Bar<C>,
    C:Baz<u32>,
{
    assert_eq!( this.field_(names::c), &100 );
    assert_eq!( this.field_(names::d), &101 );
}

fn main(){
    assert_nested(&make_struct!{
        foo:make_struct!{
            boo:100,
            bar:make_struct!{
                baz:101,
            }
        }
    });
}