Function synstructure::match_pattern [] [src]

pub fn match_pattern<'a, N: ToTokens>(name: &N,
                                      vd: &'a VariantData,
                                      options: &BindOpts)
                                      -> (Tokens, Vec<BindingInfo<'a>>)

Generate a match pattern for binding to the given VariantData This function returns a tuple of the tokens which make up that match pattern, and a BindingInfo object for each of the bindings which were made. The bind parameter controls the type of binding which is made.

The BindingInfo object holds a mutable reference into the original VariantData, 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_pattern, BindStyle};

fn main() {
    let ast = syn::parse_macro_input("struct A { a: i32, b: i32 }").unwrap();
    let vd = if let syn::Body::Struct(ref vd) = ast.body {
        vd
    } else { unreachable!() };

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