Function synstructure::each_field
[−]
[src]
pub fn each_field<F, T: ToTokens>(input: &mut MacroInput, bind: BindStyle, func: F) -> Tokens where F: Fn(BindingInfo) -> T
This method calls func
once per field in the struct or enum, and generates
a series of match branches which will destructure match argument, and run
the result of func
once on each of the bindings.
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::{each_field, BindStyle}; fn main() { let mut ast = syn::parse_macro_input("struct A { a: i32, b: i32 }").unwrap(); let tokens = each_field(&mut ast, BindStyle::Ref, |bi| quote! { println!("Saw: {:?}", #bi); }); let e = concat!("A { a : ref __binding_0 , b : ref __binding_1 , } ", "=> { ", "{ println ! ( \"Saw: {:?}\" , __binding_0 ) ; } ", "{ println ! ( \"Saw: {:?}\" , __binding_1 ) ; } ", "( ) } "); assert_eq!(&tokens.to_string(), e); }Run