crates.io

addgetterssetters

Makes it much easier to add getters and setters for fields of structures.

Done by simply just adding the appropriate meta tag(s) onto the struct or fields of the struct which will generate the corresponding methods for you.

usage

| Function to derive | Tag | Signature of generated method | |--|--|--| | AddGetter | #[get] | pub fn get_{field name}(&self) -> &{field data type} | | AddGetterVal | #[getval] | pub fn {field name}(&self) -> {field data type} | | AddGetterMut | #[getmut] | pub fn get_{field name}_mut(&mut self) -> &mut {field data type} | | AddSetter | #[set] | pub fn set_{field name}(&mut self, v: {field data type}) |

Note that all generated functions are public methods.

Example

struct HorseRider {
    //stuff here
}

#[derive(AddGetter, AddGetterVal, AddGetterMut, AddSetter)]
struct RaceHorse {
    #[get]
    name: String,

    #[get]
    #[get_val]
    #[set]
    speed: i16,

    #[get]
    #[get_mut]
    rider: HorseRider,
}

With this code, these methods would be generared for you...

impl RaceHorse {
    pub fn get_name(&self) -> &String {
        &self.name
    }

    pub fn get_speed(&self) -> &i16 {
        &self.speed
    }

    pub fn speed(&self) -> i16 {
        self.speed // implicit copy here
    }

    pub fn set_speed(&mut self, v: i16) {
        self.speed = v;
    }

    pub fn get_rider(&self) -> &HorseRider {
        &self.rider
    }

    pub fn get_rider_mut(&mut self) -> &mut HorseRider {
        &mut self.rider
    }
}

Since version 1.0.0

Add a getter and a setter for every field by adding the #[get] and #[set] tags to the struct definition:

#[derive(AddGetter, AddSetter)]
#[get]
#[set]
struct Dragon {
    name: String,
    age: u64,
    weight: u32
}

which has the same effect as doing the following:

#[derive(AddGetter, AddSetter)]
struct Dragon {
    #[get]
    #[set]
    name: String,

    #[get]
    #[set]
    age: u64,

    #[get]
    #[set]
    weight: u32
}