[][src]Type Definition structural::field::GetFieldType

type GetFieldType<This, FieldPath> = <This as FieldType<FieldPath>>::Ty;

Queries the type of a field.

Example

Here is one way you can get the type of a struct field.

use structural::{GetField,StructuralExt,GetFieldType,FP,fp};

fn get_name<'a,T>(this:&'a T)->&'a GetFieldType<T,FP!(name)>
where
    T:GetField<FP!(name)>,
{
    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,StructuralExt,GetFieldType,FP,fp};

fn get_name<T,O>(this:&T)->&O
where
    T:GetField<FP!(name), 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).

Example

Here's an example of accessing an enum field,using GetFieldType to get the field type.

This also demonstrates a way to write extension traits.

use structural::{FP, StructuralExt, GetFieldType, GetVariantField, Structural, TS, fp};
use structural::for_examples::EnumOptA;

let foo= EnumOptA::Limbs{legs:Some(9), hands:None};
assert_eq!( foo.get_limbs(), Some((&Some(9), &None)) );

let array=[0,1,2,3];
let baz=EnumGround::Limbs{legs:"many", hands:&array};
assert_eq!( baz.get_limbs(), Some((&"many", &&array[..])) );

trait GetLimbs:
    GetVariantField<TS!(Limbs),TS!(legs)> +
    GetVariantField<TS!(Limbs),TS!(hands)>
{
    fn get_limbs(&self)-> Option<(
        &GetFieldType<Self, FP!(::Limbs.legs)>,
        &GetFieldType<Self, FP!(::Limbs.hands)>,
    )> {
        self.fields(fp!(::Limbs=>legs,hands))
    }
}

impl<T> GetLimbs for T
where
    T: ?Sized +
       GetVariantField<TS!(Limbs),TS!(legs)> +
       GetVariantField<TS!(Limbs),TS!(hands)>
{}


#[derive(Structural, Copy, Clone, Debug, PartialEq)]
#[struc(no_trait)]
pub enum EnumGround<'a> {
    Limbs {
        legs: &'static str,
        hands: &'a [u8],
    },
}