[][src]Derive Macro getters_by_type::GettersMutByType

#[derive(GettersMutByType)]

The GettersMutByType macro automatically generates an impl for the given struct, implementing a getter method for each different type contained within the struct.

The generated methods start with the prefix get_mut_fields_ and end with a transcription of the type they refer.

Example:

use getters_by_type::GettersMutByType;
#[derive(GettersMutByType)]
struct Foo {
    a: String,
    b: String,
}

// Would generete:

impl Foo {
    fn get_mut_fields_string(&mut self) -> [&mut String; 2] {
        [&mut self.a, &mut self.b]
    }
}

This is the mutable version of GettersByType. The same rules are applying, so check the GettersByType derive documentation first.

There is one important difference, thought. There are some fields with types that can't be made mutable. I.e. types with immutable references. When that's the case, the field gets ignored completely.

Example:

use getters_by_type::GettersMutByType;
#[derive(GettersMutByType)]
struct Foo<'a> {
    a: &'a String,
    b: String,
}

let string = String::new();
let mut o = Foo {
    a: &string,
    b: "".into(),
};

assert_eq!(o.get_mut_fields_string().len(), 1); // instead of 2