Trait erased_serde::Serializer

source ·
pub trait Serializer: Sealed {
Show 31 methods // Required methods fn erased_serialize_bool(&mut self, v: bool); fn erased_serialize_i8(&mut self, v: i8); fn erased_serialize_i16(&mut self, v: i16); fn erased_serialize_i32(&mut self, v: i32); fn erased_serialize_i64(&mut self, v: i64); fn erased_serialize_i128(&mut self, v: i128); fn erased_serialize_u8(&mut self, v: u8); fn erased_serialize_u16(&mut self, v: u16); fn erased_serialize_u32(&mut self, v: u32); fn erased_serialize_u64(&mut self, v: u64); fn erased_serialize_u128(&mut self, v: u128); fn erased_serialize_f32(&mut self, v: f32); fn erased_serialize_f64(&mut self, v: f64); fn erased_serialize_char(&mut self, v: char); fn erased_serialize_str(&mut self, v: &str); fn erased_serialize_bytes(&mut self, v: &[u8]); fn erased_serialize_none(&mut self); fn erased_serialize_some(&mut self, value: &dyn Serialize); fn erased_serialize_unit(&mut self); fn erased_serialize_unit_struct(&mut self, name: &'static str); fn erased_serialize_unit_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, ); fn erased_serialize_newtype_struct( &mut self, name: &'static str, value: &dyn Serialize, ); fn erased_serialize_newtype_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, value: &dyn Serialize, ); fn erased_serialize_seq( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeSeq, ErrorImpl>; fn erased_serialize_tuple( &mut self, len: usize, ) -> Result<&mut dyn SerializeTuple, ErrorImpl>; fn erased_serialize_tuple_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleStruct, ErrorImpl>; fn erased_serialize_tuple_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleVariant, ErrorImpl>; fn erased_serialize_map( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeMap, ErrorImpl>; fn erased_serialize_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeStruct, ErrorImpl>; fn erased_serialize_struct_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeStructVariant, ErrorImpl>; 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 a &dyn erased_serde::Serializer or Box<dyn 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();
}

This trait is sealed and can only be implemented via a serde::Serializer impl.

Required Methods§

source

fn erased_serialize_bool(&mut self, v: bool)

source

fn erased_serialize_i8(&mut self, v: i8)

source

fn erased_serialize_i16(&mut self, v: i16)

source

fn erased_serialize_i32(&mut self, v: i32)

source

fn erased_serialize_i64(&mut self, v: i64)

source

fn erased_serialize_i128(&mut self, v: i128)

source

fn erased_serialize_u8(&mut self, v: u8)

source

fn erased_serialize_u16(&mut self, v: u16)

source

fn erased_serialize_u32(&mut self, v: u32)

source

fn erased_serialize_u64(&mut self, v: u64)

source

fn erased_serialize_u128(&mut self, v: u128)

source

fn erased_serialize_f32(&mut self, v: f32)

source

fn erased_serialize_f64(&mut self, v: f64)

source

fn erased_serialize_char(&mut self, v: char)

source

fn erased_serialize_str(&mut self, v: &str)

source

fn erased_serialize_bytes(&mut self, v: &[u8])

source

fn erased_serialize_none(&mut self)

source

fn erased_serialize_some(&mut self, value: &dyn Serialize)

source

fn erased_serialize_unit(&mut self)

source

fn erased_serialize_unit_struct(&mut self, name: &'static str)

source

fn erased_serialize_unit_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, )

source

fn erased_serialize_newtype_struct( &mut self, name: &'static str, value: &dyn Serialize, )

source

fn erased_serialize_newtype_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, value: &dyn Serialize, )

source

fn erased_serialize_seq( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeSeq, ErrorImpl>

source

fn erased_serialize_tuple( &mut self, len: usize, ) -> Result<&mut dyn SerializeTuple, ErrorImpl>

source

fn erased_serialize_tuple_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleStruct, ErrorImpl>

source

fn erased_serialize_tuple_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleVariant, ErrorImpl>

source

fn erased_serialize_map( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeMap, ErrorImpl>

source

fn erased_serialize_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeStruct, ErrorImpl>

source

fn erased_serialize_struct_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeStructVariant, ErrorImpl>

source

fn erased_is_human_readable(&self) -> bool

Implementations§

source§

impl dyn Serializer

source

pub fn erase<S>(serializer: S) -> impl Serializer
where S: Serializer,

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();
}

Implementations on Foreign Types§

source§

impl<T> Serializer for &mut T
where T: ?Sized + Serializer,

source§

fn erased_serialize_bool(&mut self, v: bool)

source§

fn erased_serialize_i8(&mut self, v: i8)

source§

fn erased_serialize_i16(&mut self, v: i16)

source§

fn erased_serialize_i32(&mut self, v: i32)

source§

fn erased_serialize_i64(&mut self, v: i64)

source§

fn erased_serialize_i128(&mut self, v: i128)

source§

fn erased_serialize_u8(&mut self, v: u8)

source§

fn erased_serialize_u16(&mut self, v: u16)

source§

fn erased_serialize_u32(&mut self, v: u32)

source§

fn erased_serialize_u64(&mut self, v: u64)

source§

fn erased_serialize_u128(&mut self, v: u128)

source§

fn erased_serialize_f32(&mut self, v: f32)

source§

fn erased_serialize_f64(&mut self, v: f64)

source§

fn erased_serialize_char(&mut self, v: char)

source§

fn erased_serialize_str(&mut self, v: &str)

source§

fn erased_serialize_bytes(&mut self, v: &[u8])

source§

fn erased_serialize_none(&mut self)

source§

fn erased_serialize_some(&mut self, value: &dyn Serialize)

source§

fn erased_serialize_unit(&mut self)

source§

fn erased_serialize_unit_struct(&mut self, name: &'static str)

source§

fn erased_serialize_unit_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, )

source§

fn erased_serialize_newtype_struct( &mut self, name: &'static str, value: &dyn Serialize, )

source§

fn erased_serialize_newtype_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, value: &dyn Serialize, )

source§

fn erased_serialize_seq( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeSeq, ErrorImpl>

source§

fn erased_serialize_tuple( &mut self, len: usize, ) -> Result<&mut dyn SerializeTuple, ErrorImpl>

source§

fn erased_serialize_tuple_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleStruct, ErrorImpl>

source§

fn erased_serialize_tuple_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleVariant, ErrorImpl>

source§

fn erased_serialize_map( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeMap, ErrorImpl>

source§

fn erased_serialize_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeStruct, ErrorImpl>

source§

fn erased_serialize_struct_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeStructVariant, ErrorImpl>

source§

fn erased_is_human_readable(&self) -> bool

source§

impl<T> Serializer for Box<T>
where T: ?Sized + Serializer,

source§

fn erased_serialize_bool(&mut self, v: bool)

source§

fn erased_serialize_i8(&mut self, v: i8)

source§

fn erased_serialize_i16(&mut self, v: i16)

source§

fn erased_serialize_i32(&mut self, v: i32)

source§

fn erased_serialize_i64(&mut self, v: i64)

source§

fn erased_serialize_i128(&mut self, v: i128)

source§

fn erased_serialize_u8(&mut self, v: u8)

source§

fn erased_serialize_u16(&mut self, v: u16)

source§

fn erased_serialize_u32(&mut self, v: u32)

source§

fn erased_serialize_u64(&mut self, v: u64)

source§

fn erased_serialize_u128(&mut self, v: u128)

source§

fn erased_serialize_f32(&mut self, v: f32)

source§

fn erased_serialize_f64(&mut self, v: f64)

source§

fn erased_serialize_char(&mut self, v: char)

source§

fn erased_serialize_str(&mut self, v: &str)

source§

fn erased_serialize_bytes(&mut self, v: &[u8])

source§

fn erased_serialize_none(&mut self)

source§

fn erased_serialize_some(&mut self, value: &dyn Serialize)

source§

fn erased_serialize_unit(&mut self)

source§

fn erased_serialize_unit_struct(&mut self, name: &'static str)

source§

fn erased_serialize_unit_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, )

source§

fn erased_serialize_newtype_struct( &mut self, name: &'static str, value: &dyn Serialize, )

source§

fn erased_serialize_newtype_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, value: &dyn Serialize, )

source§

fn erased_serialize_seq( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeSeq, ErrorImpl>

source§

fn erased_serialize_tuple( &mut self, len: usize, ) -> Result<&mut dyn SerializeTuple, ErrorImpl>

source§

fn erased_serialize_tuple_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleStruct, ErrorImpl>

source§

fn erased_serialize_tuple_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeTupleVariant, ErrorImpl>

source§

fn erased_serialize_map( &mut self, len: Option<usize>, ) -> Result<&mut dyn SerializeMap, ErrorImpl>

source§

fn erased_serialize_struct( &mut self, name: &'static str, len: usize, ) -> Result<&mut dyn SerializeStruct, ErrorImpl>

source§

fn erased_serialize_struct_variant( &mut self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<&mut dyn SerializeStructVariant, ErrorImpl>

source§

fn erased_is_human_readable(&self) -> bool

Implementors§