[−][src]Macro structural::structural_alias
The structural_alias
defines a trait alias for multiple field accessors.
The entire syntax
structural_alias!{ pub trait Foo<'a,T:Copy>:SuperTrait where T:SuperTrait { a:u32, ref b:T, mut c:i64, move d:String, mut move e:String, } }
Outside of the {...}
the trait syntax is the same as the
regular one,with the same meaning.
Inside the {...}
is a list of fields,
each of which get turned into supertraits on Foo
:
-
a:u32
: Corresponds to theGetField<TString<(_a,)>,Ty=u32>
shared reference field accessor trait. -
ref b:T
Corresponds to theGetField<TString<(_b,)>,Ty=T>
shared reference field accessor trait. -
mut c:i64
: Corresponds to theGetFieldMut<TString<(_c,)>,Ty=i64>
mutable reference field accessor trait (whichitself implies
GetField`). -
move d:String
: Corresponds to theIntoField<TString<(_d,)>,Ty=String>
by value field accessor trait (which itself impliesGetField
). -
mut move e:String
: Corresponds to theIntoFieldMut<TString<(_e,)>,Ty=String>
trait, allowing shared,mutable,and by value access to the field.
Examples
Defining a Point trait alias
use structural::{ structural_alias, ti, GetFieldExt, Structural, }; use core::{ cmp::PartialEq, fmt::{Debug,Display}, }; structural_alias!{ trait Point<T>{ mut move x:T, mut move y:T, } } fn print_point<T,U>(value:&T) where T:Point<U>, U:Debug+Display+PartialEq, { // This gets references to the `x` and `y` fields. let (x,y)=value.fields(ti!(x,y)); assert_ne!(x,y); println!("x={} y={}",x,y); } #[derive(Structural)] #[struc(access="mut move")] struct Point3D<T>{ pub x:T, pub y:T, pub z:T, } #[derive(Structural)] #[struc(access="mut move")] struct Rectangle<T>{ pub x:T, pub y:T, pub w:T, pub h:T, } #[derive(Structural)] #[struc(access="mut move")] struct Entity{ pub id:PersonId, pub x:f32, pub y:f32, } print_point(&Point3D{ x:100, y:200, z:6000 }); print_point(&Rectangle{ x:100, y:200, w:300, h:400 }); print_point(&Entity{ x:100.0, y:200.0, id:PersonId(0xDEAD) });
Defining a trait aliases with all accessibilities
use structural::{ structural_alias, ti, GetFieldExt, }; structural_alias!{ trait Person{ // shared access (a & reference to the field) id:PersonId, // shared access (a & reference to the field) name:String, // mutable access (a &mut reference to the field),as well as shared access. mut friends:Vec<PersonId>, // by value access to the field (as well as shared) move candy:Candy, // by value access to the field (as well as shared and mutable) mut move snack:Snack, } }