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

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§

source

fn read_to_vec<T, F>( &mut self, f: F ) -> Result<Vec<T>, <Self as Decoder>::Error>where F: FnMut(&mut Self) -> Result<T, <Self as Decoder>::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() })
                }))
            })
        })
    }
}

Object Safety§

This trait is not object safe.

Implementors§