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>,
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