Struct Iterable
Struct Iterable
is a Rust library that provides a proc macro to make a struct iterable. This allows you to iterate over the fields of your struct in a generic way, with each iteration returning a tuple containing the name of the field as a static string and a reference to the field's value as a dyn Any
.
How to Use
First, add Struct Iterable
to your Cargo.toml
:
[]
= "0.1.1"
Next, include the library at the top of your Rust file:
use Iterable;
Finally, add the #[derive(Iterable)]
attribute to your struct:
Now, you can iterate over the fields of an instance of your struct:
let my_instance = MyStruct ;
for in my_instance.iter
Limitations
- Only structs with named fields are supported.
- Only structs are supported, not enums or unions.
Implementation
Here is the implementation of the proc macro:
extern crate proc_macro;
use TokenStream;
use quote;
use ;
use Iterable;
The macro takes in the TokenStream of a struct and expands it into an implementation of the Iterable trait for that struct. This trait provides an iter method that returns an iterator over tuples of field names and values.
Contributing and License
Struct Iterable
is an open-source project, and contributions are warmly welcomed. Whether you're fixing bugs, improving the documentation, or proposing new features, your efforts are highly appreciated!
If you're interested in contributing, please feel free to submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project, you agree to abide by its terms.
Struct Iterable
is distributed under the terms of the MIT license. As such, you're free to use, modify, distribute, and privately use it in any way you see fit, in accordance with the terms of the license.