NOTE: What follows is an exerpt from the module level documentation. For full details read the docs on docs.rs
This crate provides helper types for matching against enum variants, and extracting bindings to each of the fields in the deriving Struct or Enum in a generic way.
If you are writing a #[derive]
which needs to perform some operation on
every field, then you have come to the right place!
WalkFields
rust
pub trait WalkFields: std::any::Any {
fn walk_fields(&self, walk: &mut FnMut(&WalkFields));
}
impl WalkFields for i32 {
fn walk_fields(&self, _walk: &mut FnMut(&WalkFields)) {}
}
```rust
extern crate synstructure;
extern crate quote;
fn walkfields_derive(s: synstructure::Structure) -> quote::Tokens { let body = s.each(|bi| quote!{ walk(#bi) });
s.bound_impl(quote!(example_traits::WalkFields), quote!{
fn walk_fields(&self, walk: &mut FnMut(&example_traits::WalkFields)) {
match *self { #body }
}
})
} declderive!([WalkFields] => walkfieldsderive);
/*
* Test Case
*/
fn main() {
testderive! {
walkfieldsderive {
enum A
Interest
rust
pub trait Interest {
fn interesting(&self) -> bool;
}
impl Interest for i32 {
fn interesting(&self) -> bool { *self > 0 }
}
```rust
extern crate synstructure;
extern crate quote;
fn interestderive(mut s: synstructure::Structure) -> quote::Tokens { let body = s.fold(false, |acc, bi| quote!{ #acc || exampletraits::Interest::interesting(#bi) });
s.bound_impl(quote!(example_traits::Interest), quote!{
fn interesting(&self) -> bool {
match *self {
#body
}
}
})
} declderive!([Interest] => interestderive);
/*
* Test Case
*/
fn main() {
testderive!{
interestderive {
enum A
For more example usage, consider investigating the abomonation_derive
crate,
which makes use of this crate, and is fairly simple.