# Struct synstructure::VariantInfo [−] [src]

    pub struct VariantInfo<'a> {
        pub prefix: Option<&'a Ident>,
        // some fields omitted
    }

A wrapper around a `syn` `DeriveInput`'s variant which provides utilities for destructuring `Variant`s with `match` expressions.

## Fields

`prefix: Option<&'a Ident>`

## Methods

### `impl<'a> VariantInfo<'a>`
[src]

#### `pub fn bindings(&self) -> &[BindingInfo<'a>]`
[src]

Returns a slice of the bindings in this Variant.

#### `pub fn bindings_mut(&mut self) -> &mut [BindingInfo<'a>]`
[src]

Returns a mut slice of the bindings in this Variant.

#### `pub fn ast(&self) -> VariantAst<'a>`
[src]

Returns a `VariantAst` object which contains references to the underlying `syn` AST node which this `Variant` was created from.

#### `pub fn omitted_bindings(&self) -> bool`
[src]

True if any bindings were omitted due to a `filter` call.

#### `pub fn pat(&self) -> Tokens`
[src]

Generates the match-arm pattern which could be used to match against this Variant.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B(i32, i32),
            C(u32),
        }
    "#).unwrap();
    let s = Structure::new(&di);

    assert_eq!(
        s.variants()[0].pat(),
        quote!{
            A::B(ref __binding_0, ref __binding_1,)
        }
    );

#### `pub fn construct<F, T>(&self, func: F) -> Tokens where
F: FnMut(&Field, usize) -> T,
T: ToTokens,`
[src]

Generates the token stream required to construct the current variant.

The init array initializes each of the fields in the order they are written in `variant.ast().fields`.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B(usize, usize),
            C{ v: usize },
        }
    "#).unwrap();
    let s = Structure::new(&di);

    assert_eq!(
        s.variants()[0].construct(|_, i| quote!(#i)),

        quote!{
            A::B(0usize, 1usize,)
        }
    );

    assert_eq!(
        s.variants()[1].construct(|_, i| quote!(#i)),

        quote!{
            A::C{ v: 0usize, }
        }
    );

#### `pub fn each<F, R>(&self, f: F) -> Tokens where
F: FnMut(&BindingInfo) -> R,
R: ToTokens,`
[src]

Runs the passed-in function once for each bound field, passing in a `BindingInfo`. and generating a `match` arm which evaluates the returned tokens.

This method will ignore fields which are ignored through the `filter` method.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B(i32, i32),
            C(u32),
        }
    "#).unwrap();
    let s = Structure::new(&di);

    assert_eq!(
        s.variants()[0].each(|bi| quote!(println!("{:?}", #bi))),

        quote!{
            A::B(ref __binding_0, ref __binding_1,) => {
                { println!("{:?}", __binding_0) }
                { println!("{:?}", __binding_1) }
            }
        }
    );

#### `pub fn fold<F, I, R>(&self, init: I, f: F) -> Tokens where
F: FnMut(Tokens, &BindingInfo) -> R,
I: ToTokens,
R: ToTokens,`
[src]

Runs the passed-in function once for each bound field, passing in the result of the previous call, and a `BindingInfo`. generating a `match` arm which evaluates to the resulting tokens.

This method will ignore fields which are ignored through the `filter` method.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B(i32, i32),
            C(u32),
        }
    "#).unwrap();
    let s = Structure::new(&di);

    assert_eq!(
        s.variants()[0].fold(quote!(0), |acc, bi| quote!(#acc + #bi)),

        quote!{
            A::B(ref __binding_0, ref __binding_1,) => {
                0 + __binding_0 + __binding_1
            }
        }
    );

#### `pub fn filter<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> bool,`
[src]

Filter the bindings created by this `Variant` object. This has 2 effects:

- The bindings will no longer appear in match arms generated by methods on this `Variant` or its subobjects.

- Impl blocks created with the `bound_impl` or `unsafe_bound_impl` method only consider type parameters referenced in the types of non-filtered fields.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B{ a: i32, b: i32 },
            C{ a: u32 },
        }
    "#).unwrap();
    let mut s = Structure::new(&di);

    s.variants_mut()[0].filter(|bi| {
        bi.ast().ident == Some("b".into())
    });

    assert_eq!(
        s.each(|bi| quote!(println!("{:?}", #bi))),

        quote!{
            A::B{ b: ref __binding_1, .. } => {
                { println!("{:?}", __binding_1) }
            }
            A::C{ a: ref __binding_0, } => {
                { println!("{:?}", __binding_0) }
            }
        }
    );

#### `pub fn remove_binding(&mut self, idx: usize) -> &mut Self`
[src]

Remove the binding at the given index.

# Panics

Panics if the index is out of range.

#### `pub fn bind_with<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&BindingInfo) -> BindStyle,`
[src]

Updates the `BindStyle` for each of the passed-in fields by calling the passed-in function for each `BindingInfo`.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B(i32, i32),
            C(u32),
        }
    "#).unwrap();
    let mut s = Structure::new(&di);

    s.variants_mut()[0].bind_with(|bi| BindStyle::RefMut);

    assert_eq!(
        s.each(|bi| quote!(println!("{:?}", #bi))),

        quote!{
            A::B(ref mut __binding_0, ref mut __binding_1,) => {
                { println!("{:?}", __binding_0) }
                { println!("{:?}", __binding_1) }
            }
            A::C(ref __binding_0,) => {
                { println!("{:?}", __binding_0) }
            }
        }
    );

#### `pub fn binding_name<F>(&mut self, f: F) -> &mut Self where
F: FnMut(&Field, usize) -> Ident,`
[src]

Updates the binding name for each fo the passed-in fields by calling the passed-in function for each `BindingInfo`.

The function will be called with the `BindingInfo` and its index in the enclosing variant.

The default name is `__binding_{}` where `{}` is replaced with an increasing number.

# Example

    let di = syn::parse_derive_input(r#"
        enum A {
            B{ a: i32, b: i32 },
            C{ a: u32 },
        }
    "#).unwrap();
    let mut s = Structure::new(&di);

    s.variants_mut()[0].binding_name(|bi, i| bi.ident.clone().unwrap());

    assert_eq!(
        s.each(|bi| quote!(println!("{:?}", #bi))),

        quote!{
            A::B{ a: ref a, b: ref b, } => {
                { println!("{:?}", a) }
                { println!("{:?}", b) }
            }
            A::C{ a: ref __binding_0, } => {
                { println!("{:?}", __binding_0) }
            }
        }
    );

#### `pub fn referenced_ty_params(&self) -> Vec<&'a Ident>`
[src]

Returns a list of the type parameters which are referenced in this field's type.

# Caveat

If the field contains any macros in type position, all parameters will be considered bound. This is because we cannot determine which type parameters are bound by type macros.

# Example

    let di = syn::parse_derive_input(r#"
        struct A<T, U> {
            a: Option<T>,
            b: U,
        }
    "#).unwrap();
    let mut s = Structure::new(&di);

    assert_eq!(
        s.variants()[0].bindings()[0].referenced_ty_params(),
        &[&(syn::Ident::from("T"))]
    );

## Trait Implementations

### `impl<'a> Debug for VariantInfo<'a>`
[src]

#### `fn fmt(&self, __arg_0: &mut Formatter) -> Result`
[src]

Formats the value using the given formatter. Read more

### `impl<'a> Clone for VariantInfo<'a>`
[src]

#### `fn clone(&self) -> VariantInfo<'a>`
[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`
1.0.0
[src]

Performs copy-assignment from `source`. Read more

### `impl<'a> PartialEq for VariantInfo<'a>`
[src]

#### `fn eq(&self, __arg_0: &VariantInfo<'a>) -> bool`
[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `fn ne(&self, __arg_0: &VariantInfo<'a>) -> bool`
[src]

This method tests for `!=`.

### `impl<'a> Eq for VariantInfo<'a>`
[src]

### `impl<'a> Hash for VariantInfo<'a>`
[src]

#### `fn hash<__H: Hasher>(&self, __arg_0: &mut __H)`
[src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,`
1.3.0
[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)