macro_rules! FP {
($ident:ident) => { ... };
(0) => { ... };
(1) => { ... };
(2) => { ... };
(3) => { ... };
(4) => { ... };
(5) => { ... };
(6) => { ... };
(7) => { ... };
(8) => { ... };
(9) => { ... };
(_) => { ... };
($lit:literal) => { ... };
($($everything:tt)*) => { ... };
}Expand description
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.
§Struct Example
use structural::{GetField, StructuralExt, FP, fp, make_struct};
greet_entity(&make_struct!{ name: "Bob" }, &(99,999,999));
type Path_0 = FP!(0);
// Equivalent to `type Path_name = FP!(name);`,
// the `FP` and `fp` macros use string literals to emulate non-ascii identifiers.
type Path_name = FP!("name");
fn greet_entity<S, This, Tup>(entity:&This, tuple:&Tup)
where
This: GetField<FP!(name), Ty = S>,
Tup : GetField<Path_0, Ty = u64>,
S: AsRef<str>,
{
assert_eq!( entity.field_(fp!(name)).as_ref(), "Bob" );
assert_eq!( entity.field_(Path_name::NEW).as_ref(), "Bob" );
assert_eq!( tuple.field_(fp!(0)), &99 );
assert_eq!( tuple.field_(Path_0::NEW), &99 );
}
§Enum Example
use structural::{GetVariantField, Structural, StructuralExt, FP, TS};
assert_eq!( get_number(&Enum::Foo(10)), Some(10) );
assert_eq!( get_number(&Enum::Bar{value: 20}), Some(20) );
assert_eq!( get_number(&Other::Foo(30, "foo")), Some(30) );
assert_eq!( get_number(&Other::Bar{value: 40, uh: None}), Some(40) );
assert_eq!( get_number(&Other::Baz), None );
type Path_Foo_0 = FP!(::Foo.0);
// Equivalent to `type Path_name = FP!(::Bar.value);`,
// the `FP` and `fp` macros use string literals to emulate non-ascii identifiers.
type Path_Bar_value = FP!(::Bar."value");
fn get_number<This>(this: &This)->Option<u32>
where
// The `*VariantField*` traits require that you pass a `TStr`,
// using the `TS` macro or a type alias.
This: GetVariantField<TS!(Foo), TS!(0), Ty = u32> +
GetVariantField<TS!(Bar), TS!(value), Ty = u32>,
{
this.field_(Path_Foo_0::NEW)
.or(this.field_(Path_Bar_value::NEW))
.cloned()
}
#[derive(Structural)]
enum Enum{
Foo(u32),
Bar{value: u32},
}
#[derive(Structural)]
enum Other{
Foo(u32, &'static str),
Bar{value: u32, uh: Option<u32>},
Baz,
}