HasField

Trait HasField 

Source
pub trait HasField<Tag> {
    type Value;

    // Required method
    fn get_field(&self, _tag: PhantomData<Tag>) -> &Self::Value;
}
Expand description

The HasField trait is used to implement getter methods for a type that derives the trait.

When a struct uses #[derive(HasField)], the macro would generate a HasField implementation for each field in the struct, with the field name becoming a type-level string to be used in the generic Tag parameter.

§Example

Given the following struct:

#[derive(HasField)]
pub struct Person {
    pub name: String,
    pub age: u8,
}

The macro would generate the following implementation:

impl HasField<Symbol!("name")> for Person {
    type Value = String;

    fn get_field(&self, _tag: PhantomData<Symbol!("name")>) -> &Self::Value {
        &self.name
    }
}

impl HasField<Symbol!("age")> for Person {
    type Value = u8;

    fn get_field(&self, _tag: PhantomData<Symbol!("age")>) -> &Self::Value {
        &self.age
    }
}

Required Associated Types§

Required Methods§

Source

fn get_field(&self, _tag: PhantomData<Tag>) -> &Self::Value

Implementors§

Source§

impl<Context, Tag, Target, Value> HasField<Tag> for Context
where Context: DerefMap<Target = Target>, Target: HasField<Tag, Value = Value>,

Source§

type Value = Value