pub trait GetField<FieldName>: FieldType<FieldName> {
// Required method
fn get_field_(&self, field_name: FieldName) -> &Self::Ty;
}Expand description
Provides shared access to the FieldName field.
FieldName is expected to be a TStr.
§Safety
While this trait is not unsafe, implementors ought not mutate fields inside accessor trait impls.
Mutating fields is only advisable if those fields don’t have field accessor impls.
§Usage as Bound Example
use structural::{StructuralExt,GetField,FP,fp};
fn example(this:impl GetField<FP!(0), Ty=u32> + GetField<FP!(1), Ty=&'static str>){
assert_eq!( this.field_(fp!(0)), &99 );
assert_eq!( this.field_(fp!(1)), &"world" );
assert_eq!( this.fields(fp!(0,1)), (&99, &"world") );
assert_eq!( this.cloned_fields(fp!(0,1)), (99, "world") );
}
example((99,"world",));
example((99,"world",100,101,102,));
§Manual Implementation Example
While this trait is intended to be implemented using the Structural derive macro,
you can manually implement it like this:
use structural::{FieldType,GetField,Structural,FP};
struct Huh<T>{
value:T,
}
impl<T> Structural for Huh<T>{}
impl<T> FieldType<FP!(value)> for Huh<T>{
type Ty=T;
}
impl<T> GetField<FP!(value)> for Huh<T>{
fn get_field_(&self,_:FP!(value))->&Self::Ty{
&self.value
}
}
Required Methods§
Sourcefn get_field_(&self, field_name: FieldName) -> &Self::Ty
fn get_field_(&self, field_name: FieldName) -> &Self::Ty
Accesses the FieldName field by reference.