pub trait Serializer {
Show 31 methods fn erased_serialize_bool(&mut self, v: bool) -> Result<Ok, Error>; fn erased_serialize_i8(&mut self, v: i8) -> Result<Ok, Error>; fn erased_serialize_i16(&mut self, v: i16) -> Result<Ok, Error>; fn erased_serialize_i32(&mut self, v: i32) -> Result<Ok, Error>; fn erased_serialize_i64(&mut self, v: i64) -> Result<Ok, Error>; fn erased_serialize_u8(&mut self, v: u8) -> Result<Ok, Error>; fn erased_serialize_u16(&mut self, v: u16) -> Result<Ok, Error>; fn erased_serialize_u32(&mut self, v: u32) -> Result<Ok, Error>; fn erased_serialize_u64(&mut self, v: u64) -> Result<Ok, Error>; fn erased_serialize_i128(&mut self, v: i128) -> Result<Ok, Error>; fn erased_serialize_u128(&mut self, v: u128) -> Result<Ok, Error>; fn erased_serialize_f32(&mut self, v: f32) -> Result<Ok, Error>; fn erased_serialize_f64(&mut self, v: f64) -> Result<Ok, Error>; fn erased_serialize_char(&mut self, v: char) -> Result<Ok, Error>; fn erased_serialize_str(&mut self, v: &str) -> Result<Ok, Error>; fn erased_serialize_bytes(&mut self, v: &[u8]) -> Result<Ok, Error>; fn erased_serialize_none(&mut self) -> Result<Ok, Error>; fn erased_serialize_some(&mut self, v: &dyn Serialize) -> Result<Ok, Error>; fn erased_serialize_unit(&mut self) -> Result<Ok, Error>; fn erased_serialize_unit_struct(
        &mut self,
        name: &'static str
    ) -> Result<Ok, Error>; fn erased_serialize_unit_variant(
        &mut self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str
    ) -> Result<Ok, Error>; fn erased_serialize_newtype_struct(
        &mut self,
        name: &'static str,
        v: &dyn Serialize
    ) -> Result<Ok, Error>; fn erased_serialize_newtype_variant(
        &mut self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        v: &dyn Serialize
    ) -> Result<Ok, Error>; fn erased_serialize_seq(
        &mut self,
        len: Option<usize>
    ) -> Result<Seq<'_>, Error>; fn erased_serialize_tuple(&mut self, len: usize) -> Result<Tuple<'_>, Error>; fn erased_serialize_tuple_struct(
        &mut self,
        name: &'static str,
        len: usize
    ) -> Result<TupleStruct<'_>, Error>; fn erased_serialize_tuple_variant(
        &mut self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        len: usize
    ) -> Result<TupleVariant<'_>, Error>; fn erased_serialize_map(
        &mut self,
        len: Option<usize>
    ) -> Result<Map<'_>, Error>; fn erased_serialize_struct(
        &mut self,
        name: &'static str,
        len: usize
    ) -> Result<Struct<'_>, Error>; fn erased_serialize_struct_variant(
        &mut self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        len: usize
    ) -> Result<StructVariant<'_>, Error>; fn erased_is_human_readable(&self) -> bool;
}
Expand description

An object-safe equivalent of Serde’s Serializer trait.

Any implementation of Serde’s Serializer can be converted to an &erased_serde::Serializer or Box<erased_serde::Serializer> trait object using erased_serde::Serializer::erase.

use erased_serde::{Serialize, Serializer};
use std::collections::BTreeMap as Map;
use std::io;

fn main() {
    // Construct some serializers.
    let json = &mut serde_json::Serializer::new(io::stdout());
    let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout()));

    // The values in this map are boxed trait objects. Ordinarily this would not
    // be possible with serde::Serializer because of object safety, but type
    // erasure makes it possible with erased_serde::Serializer.
    let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
    formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
    formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));

    // These are boxed trait objects as well. Same thing here - type erasure
    // makes this possible.
    let mut values: Map<&str, Box<dyn Serialize>> = Map::new();
    values.insert("vec", Box::new(vec!["a", "b"]));
    values.insert("int", Box::new(65536));

    // Pick a Serializer out of the formats map.
    let format = formats.get_mut("json").unwrap();

    // Pick a Serialize out of the values map.
    let value = values.get("vec").unwrap();

    // This line prints `["a","b"]` to stdout.
    value.erased_serialize(format).unwrap();
}

Required Methods

Implementations

Convert any Serde Serializer to a trait object.

use erased_serde::{Serialize, Serializer};
use std::collections::BTreeMap as Map;
use std::io;

fn main() {
    // Construct some serializers.
    let json = &mut serde_json::Serializer::new(io::stdout());
    let cbor = &mut serde_cbor::Serializer::new(serde_cbor::ser::IoWrite::new(io::stdout()));

    // The values in this map are boxed trait objects. Ordinarily this would not
    // be possible with serde::Serializer because of object safety, but type
    // erasure makes it possible with erased_serde::Serializer.
    let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
    formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
    formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));

    // These are boxed trait objects as well. Same thing here - type erasure
    // makes this possible.
    let mut values: Map<&str, Box<dyn Serialize>> = Map::new();
    values.insert("vec", Box::new(vec!["a", "b"]));
    values.insert("int", Box::new(65536));

    // Pick a Serializer out of the formats map.
    let format = formats.get_mut("json").unwrap();

    // Pick a Serialize out of the values map.
    let value = values.get("vec").unwrap();

    // This line prints `["a","b"]` to stdout.
    value.erased_serialize(format).unwrap();
}

Trait Implementations

The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around. Read more
The error type when some error occurs during serialization.
Type returned from serialize_seq for serializing the content of the sequence. Read more
Type returned from serialize_tuple for serializing the content of the tuple. Read more
Type returned from serialize_tuple_struct for serializing the content of the tuple struct. Read more
Type returned from serialize_tuple_variant for serializing the content of the tuple variant. Read more
Type returned from serialize_map for serializing the content of the map. Read more
Type returned from serialize_struct for serializing the content of the struct. Read more
Type returned from serialize_struct_variant for serializing the content of the struct variant. Read more
Serialize a bool value. Read more
Serialize an i8 value. Read more
Serialize an i16 value. Read more
Serialize an i32 value. Read more
Serialize an i64 value. Read more
Serialize a u8 value. Read more
Serialize a u16 value. Read more
Serialize a u32 value. Read more
Serialize a u64 value. Read more
Serialize an i128 value. Read more
Serialize a u128 value. Read more
Serialize an f32 value. Read more
Serialize an f64 value. Read more
Serialize a character. Read more
Serialize a &str. Read more
Serialize a chunk of raw byte data. Read more
Serialize a None value. Read more
Serialize a Some(T) value. Read more
Serialize a () value. Read more
Serialize a unit struct like struct Unit or PhantomData<T>. Read more
Serialize a unit variant like E::A in enum E { A, B }. Read more
Serialize a newtype struct like struct Millimeters(u8). Read more
Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Determine whether Serialize implementations should serialize in human-readable form. Read more
Collect an iterator as a sequence. Read more
Collect an iterator as a map. Read more
Serialize a string produced by an implementation of Display. Read more
The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around. Read more
The error type when some error occurs during serialization.
Type returned from serialize_seq for serializing the content of the sequence. Read more
Type returned from serialize_tuple for serializing the content of the tuple. Read more
Type returned from serialize_tuple_struct for serializing the content of the tuple struct. Read more
Type returned from serialize_tuple_variant for serializing the content of the tuple variant. Read more
Type returned from serialize_map for serializing the content of the map. Read more
Type returned from serialize_struct for serializing the content of the struct. Read more
Type returned from serialize_struct_variant for serializing the content of the struct variant. Read more
Serialize a bool value. Read more
Serialize an i8 value. Read more
Serialize an i16 value. Read more
Serialize an i32 value. Read more
Serialize an i64 value. Read more
Serialize a u8 value. Read more
Serialize a u16 value. Read more
Serialize a u32 value. Read more
Serialize a u64 value. Read more
Serialize an i128 value. Read more
Serialize a u128 value. Read more
Serialize an f32 value. Read more
Serialize an f64 value. Read more
Serialize a character. Read more
Serialize a &str. Read more
Serialize a chunk of raw byte data. Read more
Serialize a None value. Read more
Serialize a Some(T) value. Read more
Serialize a () value. Read more
Serialize a unit struct like struct Unit or PhantomData<T>. Read more
Serialize a unit variant like E::A in enum E { A, B }. Read more
Serialize a newtype struct like struct Millimeters(u8). Read more
Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Determine whether Serialize implementations should serialize in human-readable form. Read more
Collect an iterator as a sequence. Read more
Collect an iterator as a map. Read more
Serialize a string produced by an implementation of Display. Read more
The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around. Read more
The error type when some error occurs during serialization.
Type returned from serialize_seq for serializing the content of the sequence. Read more
Type returned from serialize_tuple for serializing the content of the tuple. Read more
Type returned from serialize_tuple_struct for serializing the content of the tuple struct. Read more
Type returned from serialize_tuple_variant for serializing the content of the tuple variant. Read more
Type returned from serialize_map for serializing the content of the map. Read more
Type returned from serialize_struct for serializing the content of the struct. Read more
Type returned from serialize_struct_variant for serializing the content of the struct variant. Read more
Serialize a bool value. Read more
Serialize an i8 value. Read more
Serialize an i16 value. Read more
Serialize an i32 value. Read more
Serialize an i64 value. Read more
Serialize a u8 value. Read more
Serialize a u16 value. Read more
Serialize a u32 value. Read more
Serialize a u64 value. Read more
Serialize an i128 value. Read more
Serialize a u128 value. Read more
Serialize an f32 value. Read more
Serialize an f64 value. Read more
Serialize a character. Read more
Serialize a &str. Read more
Serialize a chunk of raw byte data. Read more
Serialize a None value. Read more
Serialize a Some(T) value. Read more
Serialize a () value. Read more
Serialize a unit struct like struct Unit or PhantomData<T>. Read more
Serialize a unit variant like E::A in enum E { A, B }. Read more
Serialize a newtype struct like struct Millimeters(u8). Read more
Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Determine whether Serialize implementations should serialize in human-readable form. Read more
Collect an iterator as a sequence. Read more
Collect an iterator as a map. Read more
Serialize a string produced by an implementation of Display. Read more
The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around. Read more
The error type when some error occurs during serialization.
Type returned from serialize_seq for serializing the content of the sequence. Read more
Type returned from serialize_tuple for serializing the content of the tuple. Read more
Type returned from serialize_tuple_struct for serializing the content of the tuple struct. Read more
Type returned from serialize_tuple_variant for serializing the content of the tuple variant. Read more
Type returned from serialize_map for serializing the content of the map. Read more
Type returned from serialize_struct for serializing the content of the struct. Read more
Type returned from serialize_struct_variant for serializing the content of the struct variant. Read more
Serialize a bool value. Read more
Serialize an i8 value. Read more
Serialize an i16 value. Read more
Serialize an i32 value. Read more
Serialize an i64 value. Read more
Serialize a u8 value. Read more
Serialize a u16 value. Read more
Serialize a u32 value. Read more
Serialize a u64 value. Read more
Serialize an i128 value. Read more
Serialize a u128 value. Read more
Serialize an f32 value. Read more
Serialize an f64 value. Read more
Serialize a character. Read more
Serialize a &str. Read more
Serialize a chunk of raw byte data. Read more
Serialize a None value. Read more
Serialize a Some(T) value. Read more
Serialize a () value. Read more
Serialize a unit struct like struct Unit or PhantomData<T>. Read more
Serialize a unit variant like E::A in enum E { A, B }. Read more
Serialize a newtype struct like struct Millimeters(u8). Read more
Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Determine whether Serialize implementations should serialize in human-readable form. Read more
Collect an iterator as a sequence. Read more
Collect an iterator as a map. Read more
Serialize a string produced by an implementation of Display. Read more

Implementations on Foreign Types

Implementors