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

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

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

### `fn emit_from_vec<T, F>(&mut self, v: &[T], f: F) -> Result<(), Self::Error> where
F: FnMut(&mut Self, &T) -> Result<(), Self::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)
                    })
                })
            })
        }
    }

## Implementors

- `impl<S: Encoder> EncoderHelpers for S`

# 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`)