[−][src]Macro structural::FP
Constructs a field path type for use as a generic parameter.
Input
This takes the same input as the fp macro, getting the type of that field path.
Examples
This demonstrates how one can bound types by the accessor traits in a where clause.
use structural::{GetField,StructuralExt,FP,fp,make_struct}; greet_entity(&make_struct!{ name: "Bob" }); fn greet_entity<This,S>(entity:&This) where This:GetField<FP!(name),Ty=S>, S:AsRef<str>, { println!("Hello, {}!",entity.field_(fp!(name)).as_ref() ); println!("Goodbye, {}!",entity.field_(fp!("name")).as_ref() ); // The two `fp!` invocations below are equivalent. // // Quotes allow for arbitrary identifiers, // useful for non-ascii identifiers before they are supported by Rust. // assert_eq!( entity.field_(fp!(name)).as_ref(), "Bob" ); assert_eq!( entity.field_(fp!("name")).as_ref(), "Bob" ); }
Example
use structural::{GetField,StructuralExt,FP,fp,make_struct}; let struc=make_struct!{ name: "Bob", huh: "John", }; greet_entity(&struc,&(99,999,999)); type Path_0=FP!(0); type Path_huh=FP!(huh); type Path_name=FP!("name"); // Equivalent to FP!(name) fn greet_entity<This,S,Tup>(entity:&This, tuple:&Tup) where This: GetField<FP!(name), Ty= S> + GetField<Path_huh, Ty= &'static str>, Tup : GetField<Path_0,Ty=u64>, S:AsRef<str>, { assert_eq!( entity.field_(fp!(name)).as_ref(), "Bob" ); assert_eq!( entity.field_(fp!("name")).as_ref(), "Bob" ); assert_eq!( entity.field_(Path_name::NEW).as_ref(), "Bob" ); assert_eq!( entity.field_(fp!(huh)), &"John" ); assert_eq!( entity.field_(fp!("huh")), &"John" ); assert_eq!( entity.field_(Path_huh::NEW), &"John" ); assert_eq!( tuple.field_(fp!(0)), &99 ); assert_eq!( tuple.field_(Path_0::NEW), &99 ); }