[−][src]Type Definition structural::field_traits::GetFieldType
type GetFieldType<This, FieldName> = <This as GetField<FieldName>>::Ty;
Queries the type of a field.
Example
Here is one way you can get the type of a field.
use structural::{GetField,GetFieldExt,GetFieldType,FP,fp}; fn get_name<T>(this:&T)->&GetFieldType<T,FP!(n a m e)> where // `FP!(n a m e)` can be written as `FP!(name)` from 1.40 onwards T:GetField<FP!(n a m e)> { this.field_(fp!(name)) } #[derive(structural::Structural)] struct Huh<T>{ #[struc(public)] #[struc(rename="name")] value:T, } fn main(){ let this=Huh{ value:"ooh".to_string() }; assert_eq!( get_name(&this), "ooh" ); }
Another way get_name could have been written is like this:
use structural::{GetField,GetFieldExt,GetFieldType,FP,fp}; fn get_name<T,O>(this:&T)->&O where // `FP!(n a m e)` can be written as `FP!(name)` from 1.40 onwards T:GetField<FP!(n a m e), Ty=O> { this.field_(fp!(name)) }
A potential downside of adding another type parameter is that it
makes it less ergonomic to specify the type of T while ignoring the field type,
since one has to write it as get_name::<Foo,_>(&foo).