David's take on Serializer

This commit is contained in:
David Tolnay
2016-07-15 00:01:31 -07:00
parent 2901344722
commit 0feeb7a341
+253 -298
View File
@@ -44,360 +44,315 @@ pub trait Serialize {
pub trait Serializer { pub trait Serializer {
/// The error type that can be returned if some error occurs during serialization. /// The error type that can be returned if some error occurs during serialization.
type Error: Error; type Error: Error;
/// A state object that is returned from `serialize_seq` and needs to be re-inserted into
/// `serialize_seq_end` when the serialization of the sequence is done /// A state object that is initialized by `serialize_seq`, passed to
/// `serialize_seq_elt`, and consumed by `serialize_seq_end`. Use `()` if no
/// state is required.
type SeqState; type SeqState;
/// A state object that is returned from `serialize_map` and needs to be re-inserted into /// A state object that is initialized by `serialize_tuple`, passed to
/// `serialize_map_end` when the serialization of the map is done /// `serialize_tuple_elt`, and consumed by `serialize_tuple_end`. Use `()`
/// if no state is required.
type TupleState;
/// A state object that is initialized by `serialize_tuple_struct`, passed
/// to `serialize_tuple_struct_elt`, and consumed by
/// `serialize_tuple_struct_end`. Use `()` if no state is required.
type TupleStructState;
/// A state object that is initialized by `serialize_tuple_variant`, passed
/// to `serialize_tuple_variant_elt`, and consumed by
/// `serialize_tuple_variant_end`. Use `()` if no state is required.
type TupleVariantState;
/// A state object that is initialized by `serialize_map`, passed to
/// `serialize_map_elt`, and consumed by `serialize_map_end`. Use `()` if no
/// state is required.
type MapState; type MapState;
/// A state object that is initialized by `serialize_struct`, passed to
/// `serialize_struct_elt`, and consumed by `serialize_struct_end`. Use `()`
/// if no state is required.
type StructState;
/// A state object that is initialized by `serialize_struct_variant`, passed
/// to `serialize_struct_variant_elt`, and consumed by
/// `serialize_struct_variant_end`. Use `()` if no state is required.
type StructVariantState;
/// Serializes a `bool` value. /// Serializes a `bool` value.
fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>; fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>;
/// Serializes a `isize` value. By default it casts the value to a `i64` and /// Serializes an `isize` value. If the format does not differentiate
/// passes it to the `serialize_i64` method. /// between `isize` and `i64`, a reasonable implementation would be to cast
#[inline] /// the value to `i64` and forward to `serialize_i64`.
fn serialize_isize(&mut self, v: isize) -> Result<(), Self::Error> { fn serialize_isize(&mut self, v: isize) -> Result<(), Self::Error>;
self.serialize_i64(v as i64)
}
/// Serializes a `i8` value. By default it casts the value to a `i64` and /// Serializes an `i8` value. If the format does not differentiate between
/// passes it to the `serialize_i64` method. /// `i8` and `i64`, a reasonable implementation would be to cast the value
#[inline] /// to `i64` and forward to `serialize_i64`.
fn serialize_i8(&mut self, v: i8) -> Result<(), Self::Error> { fn serialize_i8(&mut self, v: i8) -> Result<(), Self::Error>;
self.serialize_i64(v as i64)
}
/// Serializes a `i16` value. By default it casts the value to a `i64` and /// Serializes an `i16` value. If the format does not differentiate between
/// passes it to the `serialize_i64` method. /// `i16` and `i64`, a reasonable implementation would be to cast the value
#[inline] /// to `i64` and forward to `serialize_i64`.
fn serialize_i16(&mut self, v: i16) -> Result<(), Self::Error> { fn serialize_i16(&mut self, v: i16) -> Result<(), Self::Error>;
self.serialize_i64(v as i64)
}
/// Serializes a `i32` value. By default it casts the value to a `i64` and /// Serializes an `i32` value. If the format does not differentiate between
/// passes it to the `serialize_i64` method. /// `i32` and `i64`, a reasonable implementation would be to cast the value
#[inline] /// to `i64` and forward to `serialize_i64`.
fn serialize_i32(&mut self, v: i32) -> Result<(), Self::Error> { fn serialize_i32(&mut self, v: i32) -> Result<(), Self::Error>;
self.serialize_i64(v as i64)
}
/// Serializes a `i64` value. /// Serializes an `i64` value.
#[inline]
fn serialize_i64(&mut self, v: i64) -> Result<(), Self::Error>; fn serialize_i64(&mut self, v: i64) -> Result<(), Self::Error>;
/// Serializes a `usize` value. By default it casts the value to a `u64` and /// Serializes a `usize` value. If the format does not differentiate between
/// passes it to the `serialize_u64` method. /// `usize` and `u64`, a reasonable implementation would be to cast the
#[inline] /// value to `u64` and forward to `serialize_u64`.
fn serialize_usize(&mut self, v: usize) -> Result<(), Self::Error> { fn serialize_usize(&mut self, v: usize) -> Result<(), Self::Error>;
self.serialize_u64(v as u64)
}
/// Serializes a `u8` value. By default it casts the value to a `u64` and passes /// Serializes a `u8` value. If the format does not differentiate between
/// it to the `serialize_u64` method. /// `u8` and `u64`, a reasonable implementation would be to cast the value
#[inline] /// to `u64` and forward to `serialize_u64`.
fn serialize_u8(&mut self, v: u8) -> Result<(), Self::Error> { fn serialize_u8(&mut self, v: u8) -> Result<(), Self::Error>;
self.serialize_u64(v as u64)
}
/// Serializes a `u32` value. By default it casts the value to a `u64` and passes /// Serializes a `u16` value. If the format does not differentiate between
/// it to the `serialize_u64` method. /// `u16` and `u64`, a reasonable implementation would be to cast the value
#[inline] /// to `u64` and forward to `serialize_u64`.
fn serialize_u16(&mut self, v: u16) -> Result<(), Self::Error> { fn serialize_u16(&mut self, v: u16) -> Result<(), Self::Error>;
self.serialize_u64(v as u64)
}
/// Serializes a `u32` value. By default it casts the value to a `u64` and passes /// Serializes a `u32` value. If the format does not differentiate between
/// it to the `serialize_u64` method. /// `u32` and `u64`, a reasonable implementation would be to cast the value
#[inline] /// to `u64` and forward to `serialize_u64`.
fn serialize_u32(&mut self, v: u32) -> Result<(), Self::Error> { fn serialize_u32(&mut self, v: u32) -> Result<(), Self::Error>;
self.serialize_u64(v as u64)
}
/// `Serializes a `u64` value. /// `Serializes a `u64` value.
#[inline]
fn serialize_u64(&mut self, v: u64) -> Result<(), Self::Error>; fn serialize_u64(&mut self, v: u64) -> Result<(), Self::Error>;
/// Serializes a `f32` value. By default it casts the value to a `f64` and passes /// Serializes an `f32` value. If the format does not differentiate between
/// it to the `serialize_f64` method. /// `f32` and `f64`, a reasonable implementation would be to cast the value
#[inline] /// to `f64` and forward to `serialize_f64`.
fn serialize_f32(&mut self, v: f32) -> Result<(), Self::Error> { fn serialize_f32(&mut self, v: f32) -> Result<(), Self::Error>;
self.serialize_f64(v as f64)
}
/// Serializes a `f64` value. /// Serializes an `f64` value.
fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>; fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>;
/// Serializes a character. By default it serializes it as a `&str` containing a /// Serializes a character.
/// single character. fn serialize_char(&mut self, v: char) -> Result<(), Self::Error>;
#[inline]
fn serialize_char(&mut self, v: char) -> Result<(), Self::Error> {
self.serialize_str(::utils::encode_utf8(v).as_str())
}
/// Serializes a `&str`. /// Serializes a `&str`.
fn serialize_str(&mut self, value: &str) -> Result<(), Self::Error>; fn serialize_str(&mut self, value: &str) -> Result<(), Self::Error>;
/// Enables those serialization formats that support serializing /// Enables serializers to serialize byte slices more compactly or more
/// byte slices separately from generic arrays. By default it serializes as a regular array. /// efficiently than other types of slices. If no efficient implementation
#[inline] /// is available, a reasonable implementation would be to forward to
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> { /// `serialize_seq`.
let state = try!(self.serialize_seq(Some(value.len()))); fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>;
for b in value.iter() {
try!(self.serialize_seq_elt(b));
}
self.serialize_seq_end(Some(value.len()), state)
}
/// Serializes a `()` value. /// Serializes a `()` value.
fn serialize_unit(&mut self) -> Result<(), Self::Error>; fn serialize_unit(&mut self) -> Result<(), Self::Error>;
/// Serializes a unit struct value. /// Serializes a unit struct value. A reasonable implementation would be to
/// /// forward to `serialize_unit`.
/// By default, unit structs are serialized as a `()`. fn serialize_unit_struct(
#[inline] &mut self,
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<(), Self::Error> { name: &str,
self.serialize_unit() ) -> Result<(), Self::Error>;
}
/// Serializes a unit variant, otherwise known as a variant with no arguments. /// Serializes a unit variant, otherwise known as a variant with no
/// /// arguments. A reasonable implementation would be to forward to
/// By default, unit variants are serialized as a `()`. /// `serialize_unit`.
#[inline] fn serialize_unit_variant(
fn serialize_unit_variant(&mut self, &mut self,
_name: &'static str, name: &str,
_variant_index: usize, variant_index: usize,
_variant: &'static str) -> Result<(), Self::Error> { variant: &str,
self.serialize_unit() ) -> Result<(), Self::Error>;
}
/// Allows a tuple struct with a single element, also known as a /// Allows a tuple struct with a single element, also known as a newtype
/// newtyped value, to be more efficiently serialized than a tuple struct with multiple items. /// struct, to be more efficiently serialized than a tuple struct with
/// By default it just serializes the value as a tuple struct sequence. /// multiple items. A reasonable implementation would be to forward to
#[inline] /// `serialize_tuple_struct`.
fn serialize_newtype_struct<T>(&mut self, fn serialize_newtype_struct<T: Serialize>(
name: &'static str, &mut self,
value: T) -> Result<(), Self::Error> name: &str,
where T: Serialize, value: T,
{ ) -> Result<(), Self::Error>;
let state = try!(self.serialize_tuple_struct(name, 1));
try!(self.serialize_tuple_struct_elt(value));
self.serialize_tuple_struct_end(name, 1, state)
}
/// Allows a variant with a single item to be more efficiently /// Allows a variant with a single item to be more efficiently serialized
/// serialized than a variant with multiple items. By default it just serializes the value as a /// than a variant with multiple items. A reasonable implementation would be
/// tuple variant sequence. /// to forward to `serialize_tuple_variant`.
#[inline] fn serialize_newtype_variant<T: Serialize>(
fn serialize_newtype_variant<T>(&mut self, &mut self,
name: &'static str, name: &str,
variant_index: usize, variant_index: usize,
variant: &'static str, variant: &str,
value: T) -> Result<(), Self::Error> value: T,
where T: Serialize, ) -> Result<(), Self::Error>;
{
let state = try!(self.serialize_tuple_variant(name, variant_index, variant, 1));
try!(self.serialize_tuple_variant_elt(value));
self.serialize_tuple_variant_end(name, variant_index, variant, 1, state)
}
/// Serializes a `None` value..serialize /// Serializes a `None` value.
fn serialize_none(&mut self) -> Result<(), Self::Error>; fn serialize_none(&mut self) -> Result<(), Self::Error>;
/// Serializes a `Some(...)` value. /// Serializes a `Some(...)` value.
fn serialize_some<V>(&mut self, value: V) -> Result<(), Self::Error> fn serialize_some<T: Serialize>(
where V: Serialize; &mut self,
value: T,
) -> Result<(), Self::Error>;
/// Serializes a sequence. /// Begins to serialize a sequence. This call must be followed by zero or
/// /// more calls to `serialize_seq_elt`, then a call to `serialize_seq_end`.
/// Callees of this method need to construct a `SeqVisitor`, which iterates through each item fn serialize_seq(
/// in the sequence. &mut self,
fn serialize_seq(&mut self, len: Option<usize>) -> Result<Self::SeqState, Self::Error>; len: Option<usize>,
) -> Result<Self::SeqState, Self::Error>;
/// Serializes a sequence element. /// Serializes a sequence element. Must have previously called
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error> /// `serialize_seq`.
where T: Serialize; fn serialize_seq_elt<T: Serialize>(
&mut self,
state: &mut Self::SeqState,
value: T,
) -> Result<(), Self::Error>;
/// Finish serializing a sequence. /// Finishes serializing a sequence.
fn serialize_seq_end(&mut self, len: Option<usize>, state: Self::SeqState) -> Result<(), Self::Error>; fn serialize_seq_end(
&mut self,
state: Self::SeqState,
) -> Result<(), Self::Error>;
/// Serializes a tuple. /// Begins to serialize a tuple. This call must be followed by zero or more
/// /// calls to `serialize_tuple_elt`, then a call to `serialize_tuple_end`. A
/// By default this serializes a tuple as a sequence. /// reasonable implementation would be to forward to `serialize_seq`.
#[inline] fn serialize_tuple(
fn serialize_tuple(&mut self, len: usize) -> Result<Self::SeqState, Self::Error> &mut self,
{ len: usize,
self.serialize_seq(Some(len)) ) -> Result<Self::TupleState, Self::Error>;
}
/// Serializes a tuple element. /// Serializes a tuple element. Must have previously called
/// /// `serialize_tuple`.
/// By default, tuples are serialized as a sequence. fn serialize_tuple_elt<T: Serialize>(
#[inline] &mut self,
fn serialize_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error> state: &mut Self::TupleState,
where T: Serialize { value: T,
self.serialize_seq_elt(value) ) -> Result<(), Self::Error>;
}
/// Finishes serialization of a tuple. /// Finishes serializing a tuple.
/// fn serialize_tuple_end(
/// By default, tuples are serialized as a sequence. &mut self,
#[inline] state: Self::TupleState,
fn serialize_tuple_end(&mut self, len: usize, state: Self::SeqState) -> Result<(), Self::Error> { ) -> Result<(), Self::Error>;
self.serialize_seq_end(Some(len), state)
}
/// Serializes a fixed-size array. /// Begins to serialize a tuple struct. This call must be followed by zero
/// /// or more calls to `serialize_tuple_struct_elt`, then a call to
/// By default this serializes an array as a sequence. /// `serialize_tuple_struct_end`. A reasonable implementation would be to
#[inline] /// forward to `serialize_tuple`.
fn serialize_fixed_size_array(&mut self, size: usize) -> Result<Self::SeqState, Self::Error> fn serialize_tuple_struct(
{ &mut self,
self.serialize_seq(Some(size)) name: &str,
} len: usize,
) -> Result<Self::TupleStructState, Self::Error>;
/// Serializes a tuple struct. /// Serializes a tuple struct element. Must have previously called
/// /// `serialize_tuple_struct`.
/// By default, tuple structs are serialized as a tuple. fn serialize_tuple_struct_elt<T: Serialize>(
#[inline] &mut self,
fn serialize_tuple_struct(&mut self, state: &mut Self::TupleStructState,
_name: &'static str, value: T,
len: usize, ) -> Result<(), Self::Error>;
) -> Result<Self::SeqState, Self::Error>
{
self.serialize_tuple(len)
}
/// Serializes a tuple struct element. /// Finishes serializing a tuple struct.
/// fn serialize_tuple_struct_end(
/// By default, tuple struct elements are serialized as a tuple element. &mut self,
#[inline] state: Self::TupleStructState,
fn serialize_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Self::Error> ) -> Result<(), Self::Error>;
where T: Serialize
{
self.serialize_tuple_elt(value)
}
/// Finishes serialization of a tuple struct. /// Begins to serialize a tuple variant. This call must be followed by zero
/// /// or more calls to `serialize_tuple_variant_elt`, then a call to
/// By default, tuple structs are serialized as a sequence. /// `serialize_tuple_variant_end`. A reasonable implementation would be to
#[inline] /// forward to `serialize_tuple_struct`.
fn serialize_tuple_struct_end(&mut self, fn serialize_tuple_variant(
_name: &'static str, &mut self,
len: usize, name: &str,
state: Self::SeqState, variant_index: usize,
) -> Result<(), Self::Error> { variant: &str,
self.serialize_tuple_end(len, state) len: usize,
} ) -> Result<Self::TupleVariantState, Self::Error>;
/// Serializes a tuple variant. /// Serializes a tuple variant element. Must have previously called
/// /// `serialize_tuple_variant`.
/// By default, tuple variants are serialized as a tuple struct. fn serialize_tuple_variant_elt<T: Serialize>(
#[inline] &mut self,
fn serialize_tuple_variant(&mut self, state: &mut Self::TupleVariantState,
_name: &'static str, value: T,
_variant_index: usize, ) -> Result<(), Self::Error>;
variant: &'static str,
len: usize,
) -> Result<Self::SeqState, Self::Error>
{
self.serialize_tuple_struct(variant, len)
}
/// Serializes a tuple variant element. /// Finishes serializing a tuple variant.
/// fn serialize_tuple_variant_end(
/// By default, tuple variants are serialized as tuples. &mut self,
#[inline] state: Self::TupleVariantState,
fn serialize_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Self::Error> ) -> Result<(), Self::Error>;
where T: Serialize
{
self.serialize_tuple_struct_elt(value)
}
/// Finishes serialization of a tuple variant. /// Begins to serialize a map. This call must be followed by zero or more
/// /// calls to `serialize_map_elt`, then a call to `serialize_map_end`.
/// By default, tuple variants are serialized as tuples. fn serialize_map(
#[inline] &mut self,
fn serialize_tuple_variant_end(&mut self, len: Option<usize>,
_name: &'static str, ) -> Result<Self::MapState, Self::Error>;
_variant_index: usize,
variant: &'static str,
len: usize,
state: Self::SeqState,
) -> Result<(), Self::Error> {
self.serialize_tuple_struct_end(variant, len, state)
}
/// Serialize a map. /// Serialize a map element. Must have previously called `serialize_map`.
fn serialize_map(&mut self, len: Option<usize>) -> Result<Self::MapState, Self::Error>; fn serialize_map_elt<K: Serialize, V: Serialize>(
&mut self,
state: &mut Self::MapState,
key: K,
value: V,
) -> Result<(), Self::Error>;
/// Serialize a map element /// Finishes serializing a map.
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize; fn serialize_map_end(
&mut self,
state: Self::MapState,
) -> Result<(), Self::Error>;
/// Finishes serializing a map /// Begins to serialize a struct. This call must be followed by zero or more
fn serialize_map_end(&mut self, len: Option<usize>, state: Self::MapState) -> Result<(), Self::Error>; /// calls to `serialize_struct_elt`, then a call to `serialize_struct_end`.
fn serialize_struct(
&mut self,
name: &str,
len: usize,
) -> Result<Self::StructState, Self::Error>;
/// Serializes a struct. /// Serializes a struct field. Must have previously called
/// /// `serialize_struct`.
/// By default, structs are serialized as a map with the field name as the key. fn serialize_struct_elt<V: Serialize>(
#[inline] &mut self,
fn serialize_struct(&mut self, state: &mut Self::StructState,
_name: &'static str, key: &str,
len: usize, value: V,
) -> Result<Self::MapState, Self::Error> ) -> Result<(), Self::Error>;
{
self.serialize_map(Some(len))
}
/// Serialize a struct field /// Finishes serializing a struct.
/// fn serialize_struct_end(
/// By default, structs are serialized as a map with the field name as the key. &mut self,
fn serialize_struct_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize { state: Self::StructState,
self.serialize_map_elt(key, value) ) -> Result<(), Self::Error>;
}
/// Finishes serializing a struct /// Begins to serialize a struct variant. This call must be followed by zero
/// /// or more calls to `serialize_struct_variant_elt`, then a call to
/// By default, structs are serialized as a map with the field name as the key. /// `serialize_struct_variant_end`.
fn serialize_struct_end(&mut self, fn serialize_struct_variant(
_name: &'static str, &mut self,
len: usize, name: &str,
state: Self::MapState, variant_index: usize,
) -> Result<(), Self::Error> { variant: &str,
self.serialize_map_end(Some(len), state) len: usize,
} ) -> Result<Self::StructVariantState, Self::Error>;
/// Serializes a struct variant. /// Serialize a struct variant element. Must have previously called
/// /// `serialize_struct_variant`.
/// By default, struct variants are serialized as a struct. fn serialize_struct_variant_elt<V: Serialize>(
#[inline] &mut self,
fn serialize_struct_variant(&mut self, state: &mut Self::StructVariantState,
_name: &'static str, key: &str,
_variant_index: usize, value: V,
variant: &'static str, ) -> Result<(), Self::Error>;
len: usize,
) -> Result<Self::MapState, Self::Error>
{
self.serialize_struct(variant, len)
}
/// Serialize a struct variant element /// Finishes serializing a struct variant.
/// fn serialize_struct_variant_end(
/// By default, structs are serialized as a map with the field name as the key. &mut self,
fn serialize_struct_variant_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize { state: Self::StructVariantState,
self.serialize_struct_elt(key, value) ) -> Result<(), Self::Error>;
}
/// Finishes serializing a struct variant
///
/// By default, structs are serialized as a map with the field name as the key.
fn serialize_struct_variant_end(&mut self,
_name: &'static str,
_variant_index: usize,
variant: &'static str,
len: usize,
state: Self::MapState,
) -> Result<(), Self::Error> {
self.serialize_struct_end(variant, len, state)
}
} }