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
:
let test = Test ;
// Let's sum all the i32 field with a fold expression:
assert_eq!;
Example using GettersMutByType
:
let mut test = new;
// Let's update all the Updater fields
for updater in test.get_mut_fields_updater.iter_mut
Check more examples on the test file: tests/getters_by_type.rs
Usage
With Cargo, you can this line to your Cargo.toml:
[]
= "*"
Development
This currently works with primitive types, and many other referencial and genecic types, such as &str or Vec, but there are cases that currently are not covered, like the Fn types. Want to contribute? Pull requests are always welcome. Because this is my first work with procedural macros, I guess thinks can improve a fair lot under the hood, so there should be a few low hanging fruits already. Let's go for them!
License
MIT