[][src]Crate getters_by_type

This crate provides GettersByType and GettersMutByType derive macros for structs, which implements a getter method for each type they contain. The getter methods return an array containing references to all the fields of the same type. The GettersMutByType derive also adds a mut version for those methods.

Example using GettersByType :

use getters_by_type::GettersByType;
#[derive(GettersByType)]
struct Foo {
    first: i32,
    second: i32,
    third: i32,
}

let object = Foo { first: 6, second: 12, third: 24 };

// Let's sum all the i32 fields with a fold expression:
assert_eq!(object.get_fields_i32().iter().fold(0, |acc, x| **x + acc), 42);

For more documentation and examples, see the GettersByType derive documentation.

Example using GettersMutByType :

use getters_by_type::GettersMutByType;

#[derive(Default)]
struct Updater {}
impl Updater {
    fn update(&mut self) {/*...*/}
}

#[derive(GettersMutByType, Default)]
struct Foo {
    first: Updater,
    second: Updater,
    /*...*/
onehundredth: Updater,
}

let mut object = Foo::default();

// Let's update all the Updater fields
for updater in object.get_mut_fields_updater().iter_mut() {
updater.update();
}

For more documentation and examples, see the GettersMutByType derive documentation.

Derive Macros

GettersByType

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

GettersMutByType

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