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§
Sourcefn read_to_vec<T, F>(
&mut self,
f: F,
) -> Result<Vec<T>, <Self as Decoder>::Error>
fn read_to_vec<T, F>( &mut self, f: F, ) -> Result<Vec<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() })
}))
})
})
}
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.