This crate provides GettersByType
and GettersMutByType
derive macros for structs.
These derives implement a getter method for each type they contain.
Example using GettersByType
:
```rust
struct Test { first: i32, second: i32, third: i32, }
let test = Test { first: 6, second: 12, third: 24 };
// Let's sum all the i32 fields with a fold expression: asserteq!(test.getfields_i32().iter().fold(0, |acc, x| **x + acc), 42); ```
As you can notice, 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 GettersMutByType
:
```rust
struct Test { first: Updater, second: Updater, ... onehundredth: Updater, }
impl Updater { fn update(&mut self) {...} }
let mut test = Test::new();
// Let's update all the Updater fields for updater in test.getmutfieldsupdater().itermut() { updater.update(); } ```
Check more examples on the test file: tests/gettersbytype.rs
With Cargo, you can add this line to your Cargo.toml:
toml
[dependencies]
getters-by-type = "*"
This currently works with primitive types, and many other referencial and genecic types, such as &str
or Vec
, but there are cases that completely are not covered, like the trait objects. 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!
MIT