Function synstructure::match_substructs [] [src]

pub fn match_substructs<F, T: ToTokens>(input: &MacroInput,
                                        options: &BindOpts,
                                        func: F)
                                        -> Tokens where F: Fn(Vec<BindingInfo>) -> T

This method generates a match branch for each of the substructures of the given MacroInput. It will call func for each of these substructures, passing in the bindings which were made for each of the fields in the substructure. The return value of func is then used as the value of each branch

The BindingInfo object holds a mutable reference into the original MacroInput, which means that mutations will be reflected in the source object. This can be useful for removing attributes as they are used.

Example

extern crate syn;
extern crate synstructure;
#[macro_use]
extern crate quote;
use synstructure::{match_substructs, BindStyle};

fn main() {
    let mut ast = syn::parse_macro_input("struct A { a: i32, b: i32 }").unwrap();

    let tokens = match_substructs(&mut ast, &BindStyle::Ref.into(), |bindings| {
        assert_eq!(bindings.len(), 2);
        assert_eq!(bindings[0].ident.as_ref(), "__binding_0");
        assert_eq!(bindings[1].ident.as_ref(), "__binding_1");
        quote!("some_random_string")
    });
    assert_eq!(tokens.to_string(), quote! {
        A { a: ref __binding_0, b: ref __binding_1, } => { "some_random_string" }
    }.to_string());
}