# Trait rustc_serialize::DecoderHelpers [−] [src]

    pub trait DecoderHelpers: Decoder {
        fn read_to_vec<T, F>(&mut self, f: F) -> Result<Vec<T>, Self::Error>
        where
            F: FnMut(&mut Self) -> Result<T, Self::Error>;
    }

Trait with helper functions for implementing `Decodable`.

This trait is implemented for everything that implements `Decoder`. `Decodable` implementations can make use of it to make their implementations easier.

## Required Methods

### `fn read_to_vec<T, F>(&mut self, f: F) -> Result<Vec<T>, Self::Error> where
F: FnMut(&mut Self) -> Result<T, Self::Error>,`

Read a sequence into a vector.

Storing sequences as vectors is a common pattern. This method makes deserializing such sequences easier by wrapping the calls to `Decoder::read_seq` and `Decoder::read_seq_elt`.

# Examples

    use rustc_serialize::Decodable;
    use rustc_serialize::Decoder;
    use rustc_serialize::DecoderHelpers;

    struct NumberSequence {
        elements: Vec<i32>,
    }

    impl Decodable for NumberSequence {
        fn decode<D: Decoder>(d: &mut D) -> Result<NumberSequence, D::Error> {
            d.read_struct("NumberSequence", 2, |d| {
                Ok(NumberSequence{
                    elements: try!(d.read_struct_field("elements", 0, |d| {
                        d.read_to_vec(|d| { d.read_i32() })
                    }))
                })
            })
        }
    }

## Implementors

- `impl<D: Decoder> DecoderHelpers for D`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand 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`)