Derive Macro struct_iterable::Iterable
source · #[derive(Iterable)]
Expand description
The Iterable
proc macro.
This macro provides a convenient way to make a struct iterable.
The struct fields’ names are returned as static strings and their values as dyn Any
.
This allows to iterate over the struct fields in a generic way.
Note that only structs with named fields are supported.
Example
use struct_iterable::Iterable;
#[derive(Iterable)]
struct MyStruct {
field1: i32,
field2: String,
// etc.
}
let my_instance = MyStruct {
field1: 42,
field2: "Hello, world!".to_string(),
};
for (field_name, field_value) in my_instance.iter() {
println!("{}: {:?}", field_name, field_value);
}
The Iterable
proc macro.
Deriving this macro for your struct will make it “iterable”. An iterable struct allows you to iterate over its fields, returning a tuple containing the field name as a static string and a reference to the field’s value as dyn Any
.
Limitations
- Only structs are supported, not enums or unions.
- Only structs with named fields are supported.
Usage
Add the derive attribute (#[derive(Iterable)]
) above your struct definition.
use struct_iterable::Iterable;
#[derive(Iterable)]
struct MyStruct {
field1: i32,
field2: String,
}
You can now call the iter
method on instances of your struct to get an iterator over its fields:
let my_instance = MyStruct {
field1: 42,
field2: "Hello, world!".to_string(),
};
for (field_name, field_value) in my_instance.iter() {
println!("{}: {:?}", field_name, field_value);
}