[−][src]Macro structural::fp
Constructs a FieldPath(Set) value, which determines the fields accessed in GetFieldExt methods.
When passed a single argument,this instantiates a FieldPath,
which can be passed to the
GetFieldExt::{field_,field_mut,into_field,box_into_field} methods
to access a field.
When passed multiple arguments,this instantiates a FieldPathSet.
It can then be passed to the GetFieldExt::fields method.
To be passed to GetFieldExt::fields_mut,
FieldPathSet must be constructed with syntactically unique paths,
since there is no cheap way to check for equality of type-level strings yet.
Nested fields
You can construct field paths to access nested fields with fp!(a.b.c),
where doing this.field_(fp!(0.1.2)) is equivalent to &((this.0).1).2.
Multiple fields
You can access multiple fields simultaneously with fp!(0,1,2)
where doing this.fields_mut(fp!(a,b,c))
is equivalent to (&mut this.a,&mut this.b,&mut this.c)
Example
use structural::{GetFieldExt,fp,structural_alias}; structural_alias!{ trait Tuple3<A,B,C>{ 0:A, 1:B, 2:C, } } fn with_tuple3<'a>(tup:impl Tuple3<&'a str,&'a str,&'a str>){ assert_eq!( tup.field_(fp!(0)), &"I" ); assert_eq!( tup.field_(fp!(1)), &"you" ); assert_eq!( tup.field_(fp!(2)), &"they" ); assert_eq!( tup.fields(fp!(0,1)), (&"I",&"you") ); assert_eq!( tup.fields(fp!(0,1,2)), (&"I",&"you",&"they") ); } fn main(){ with_tuple3(("I","you","they")); with_tuple3(("I","you","they","this is not used")); with_tuple3(("I","you","they","_","this isn't used either")); }
Example
An example which accesses nested fields.
use structural::{GetFieldExt,Structural,fp,make_struct}; #[derive(Structural)] #[struc(public)] struct Foo{ bar:Bar, baz:u32, ooo:(u32,u32), } #[derive(Debug,Clone,PartialEq,Structural)] #[struc(public)] struct Bar{ aaa:(u32,u32), } fn with_foo(foo:&mut dyn Foo_SI){ let expected_bar=Bar{aaa: (300,301) }; assert_eq!( foo.field_(fp!(bar)), &expected_bar ); assert_eq!( foo.field_(fp!(bar.aaa)), &(300,301) ); assert_eq!( foo.field_(fp!(bar.aaa.0)), &300 ); assert_eq!( foo.field_(fp!(bar.aaa.1)), &301 ); assert_eq!( foo.fields_mut(fp!( bar.aaa, ooo.0, ooo.1 )), ( &mut (300,301), &mut 66, &mut 99 ) ); } fn main(){ let bar=Bar{aaa: (300,301) }; with_foo(&mut Foo{ bar:bar.clone(), baz:44, ooo:(66,99), }); with_foo(&mut make_struct!{ bar:bar.clone(), baz:44, ooo:(66,99), }); }