[][src]Macro structural::field_path_aliases

macro_rules! field_path_aliases {
    (
        $($mod_contents:tt)*
    ) => { ... };
}

Declares aliases for field paths,used to access fields.

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

As of Rust 1.39,this macro cannot be invoked within a function, you can use field_path_aliases_module within functions though.

Example

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

field_path_aliases!{
    // Equivalent to hello=hello
    hello,

    // Equivalent to world=world
    world,

    zero=0,
    one=1,
    two=2,

    // Used to access the `0`,`1`,and `2` fields
    // with the fields or fields_mut method.
    FirstThree=(0,1,2),

    h=(a,b,c),

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

}


fn assert_fields<T>(this:&T)
where
    T:GetField<zero,Ty=i32>+
        GetField<one,Ty=i32>+
        GetField<two,Ty=i32>
{
    assert_eq!( this.field_(zero), &2 );
    assert_eq!( this.field_(one), &3 );
    assert_eq!( this.field_(two), &5 );
    assert_eq!( this.fields(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,structural_alias,GetFieldExt};


field_path_aliases!{
    nested_a=0.0, // This is for accessing the `.0.0` nested field.
    nested_b=0.1, // This is for accessing the `.0.1` nested field.
    nested_c=foo.bar, // This is for accessing the `.foo.bar` nested field.
    nested_d=foo.bar.baz, // This is for accessing the `.foo.bar.baz` nested field.
}

structural_alias!{
    trait Tuple2<T>{
        ref 0:(T,T),
        ref 1:(T,T),
    }
}

fn assert_nested<T>(this:&T)
where
    T:Tuple2<u32>
{
    assert_eq!( this.field_(nested_a), &100 );
    assert_eq!( this.field_(nested_b), &101 );
}

fn main(){
    assert_nested(&(
        (100,101),
        (200,201),
    ));
}