pub trait EncoderHelpers: Encoder {
    // Required method
    fn emit_from_vec<T, F>(
        &mut self,
        v: &[T],
        f: F
    ) -> Result<(), <Self as Encoder>::Error>
       where F: FnMut(&mut Self, &T) -> Result<(), <Self as Encoder>::Error>;
}
Expand description

Trait with helper functions for implementing Encodable.

This trait is implemented for everything that implements Encoder. Encodable implementations can make use of it to make their implementations easier.

Required Methods§

source

fn emit_from_vec<T, F>( &mut self, v: &[T], f: F ) -> Result<(), <Self as Encoder>::Error>where F: FnMut(&mut Self, &T) -> Result<(), <Self as Encoder>::Error>,

Emit a vector as a sequence.

Storing sequences as vectors is a common pattern. This method makes encoding such sequences easier by wrapping the calls to Encoder::emit_seq and Encoder::emit_seq_elt.

Examples
use rustc_serialize::Encodable;
use rustc_serialize::Encoder;
use rustc_serialize::EncoderHelpers;

struct NumberSequence {
    elements: Vec<i32>,
}

impl Encodable for NumberSequence {
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_struct("NumberSequence", 1, |s| {
            s.emit_struct_field("elements", 0, |s| {
                s.emit_from_vec(&self.elements, |s,e| {
                    s.emit_i32(*e)
                })
            })
        })
    }
}

Object Safety§

This trait is not object safe.

Implementors§