Merge pull request #1 from serde-rs/dtolnay/serializer

David's take on Serializer
This commit is contained in:
Oliver Schneider 2016-07-15 10:38:22 +02:00 committed by GitHub
commit 70c83768b7
2 changed files with 320 additions and 403 deletions

View File

@ -152,11 +152,11 @@ impl<T> Serialize for [T]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer, where S: Serializer,
{ {
let state = try!(serializer.serialize_seq(Some(self.len()))); let mut state = try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() { for e in self.iter() {
try!(serializer.serialize_seq_elt(e)); try!(serializer.serialize_seq_elt(&mut state, e));
} }
serializer.serialize_seq_end(Some(self.len()), state) serializer.serialize_seq_end(state)
} }
} }
@ -169,11 +169,11 @@ macro_rules! array_impls {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer, where S: Serializer,
{ {
let state = try!(serializer.serialize_fixed_size_array($len)); let mut state = try!(serializer.serialize_seq(Some($len)));
for e in self.iter() { for e in self.iter() {
try!(serializer.serialize_seq_elt(e)); try!(serializer.serialize_seq_elt(&mut state, e));
} }
serializer.serialize_seq_end(Some(self.len()), state) serializer.serialize_seq_end(state)
} }
} }
} }
@ -215,52 +215,40 @@ array_impls!(32);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
macro_rules! serialize_seq {
() => {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let mut state = try!(serializer.serialize_seq(Some(self.len())));
for e in self {
try!(serializer.serialize_seq_elt(&mut state, e));
}
serializer.serialize_seq_end(state)
}
}
}
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for BinaryHeap<T> impl<T> Serialize for BinaryHeap<T>
where T: Serialize + Ord where T: Serialize + Ord
{ {
#[inline] serialize_seq!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(e));
}
serializer.serialize_seq_end(Some(self.len()), state)
}
} }
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for BTreeSet<T> impl<T> Serialize for BTreeSet<T>
where T: Serialize + Ord, where T: Serialize + Ord,
{ {
#[inline] serialize_seq!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(e));
}
serializer.serialize_seq_end(Some(self.len()), state)
}
} }
#[cfg(all(feature = "nightly", feature = "collections"))] #[cfg(all(feature = "nightly", feature = "collections"))]
impl<T> Serialize for EnumSet<T> impl<T> Serialize for EnumSet<T>
where T: Serialize + CLike where T: Serialize + CLike
{ {
#[inline] serialize_seq!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(e));
}
serializer.serialize_seq_end(Some(self.len()))
}
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -268,32 +256,24 @@ impl<T, H> Serialize for HashSet<T, H>
where T: Serialize + Eq + Hash, where T: Serialize + Eq + Hash,
H: BuildHasher, H: BuildHasher,
{ {
#[inline] serialize_seq!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(e));
}
serializer.serialize_seq_end(Some(self.len()), state)
}
} }
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for LinkedList<T> impl<T> Serialize for LinkedList<T>
where T: Serialize, where T: Serialize,
{ {
#[inline] serialize_seq!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> }
where S: Serializer,
{ #[cfg(any(feature = "std", feature = "collections"))]
let state = try!(serializer.serialize_seq(Some(self.len()))); impl<T> Serialize for Vec<T> where T: Serialize {
for e in self.iter() { serialize_seq!();
try!(serializer.serialize_seq_elt(e)); }
}
serializer.serialize_seq_end(Some(self.len()), state) #[cfg(any(feature = "std", feature = "collections"))]
} impl<T> Serialize for VecDeque<T> where T: Serialize {
serialize_seq!();
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
@ -306,35 +286,11 @@ impl<A> Serialize for ops::Range<A>
where S: Serializer, where S: Serializer,
{ {
let len = iter::Step::steps_between(&self.start, &self.end, &A::one()); let len = iter::Step::steps_between(&self.start, &self.end, &A::one());
let state = try!(serializer.serialize_seq(len)); let mut state = try!(serializer.serialize_seq(len));
for e in self.clone() { for e in self.clone() {
try!(serializer.serialize_seq_elt(e)); try!(serializer.serialize_seq_elt(&mut state, e));
} }
serializer.serialize_seq_end(len, state); serializer.serialize_seq_end(state)
}
}
#[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for Vec<T> where T: Serialize {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
(&self[..]).serialize(serializer)
}
}
#[cfg(any(feature = "std", feature = "collections"))]
impl<T> Serialize for VecDeque<T> where T: Serialize {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_seq(Some(self.len())));
for e in self.iter() {
try!(serializer.serialize_seq_elt(e));
}
serializer.serialize_seq_end(Some(self.len()), state)
} }
} }
@ -370,11 +326,11 @@ macro_rules! tuple_impls {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer, where S: Serializer,
{ {
let state = try!(serializer.serialize_tuple($len)); let mut state = try!(serializer.serialize_tuple($len));
$( $(
try!(serializer.serialize_tuple_elt(&e!(self.$idx))); try!(serializer.serialize_tuple_elt(&mut state, &e!(self.$idx)));
)+ )+
serializer.serialize_tuple_end($len, state) serializer.serialize_tuple_end(state)
} }
} }
)+ )+
@ -554,21 +510,27 @@ tuple_impls! {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
macro_rules! serialize_map {
() => {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let mut state = try!(serializer.serialize_map(Some(self.len())));
for (k, v) in self {
try!(serializer.serialize_map_elt(&mut state, k, v));
}
serializer.serialize_map_end(state)
}
}
}
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
impl<K, V> Serialize for BTreeMap<K, V> impl<K, V> Serialize for BTreeMap<K, V>
where K: Serialize + Ord, where K: Serialize + Ord,
V: Serialize, V: Serialize,
{ {
#[inline] serialize_map!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_map(Some(self.len())));
for (k, v) in self.iter() {
try!(serializer.serialize_map_elt(k, v));
}
serializer.serialize_map_end(Some(self.len()), state)
}
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -577,16 +539,7 @@ impl<K, V, H> Serialize for HashMap<K, V, H>
V: Serialize, V: Serialize,
H: BuildHasher, H: BuildHasher,
{ {
#[inline] serialize_map!();
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let state = try!(serializer.serialize_map(Some(self.len())));
for (k, v) in self.iter() {
try!(serializer.serialize_map_elt(k, v));
}
serializer.serialize_map_end(Some(self.len()), state)
}
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -44,360 +44,324 @@ 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: &'static 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: &'static str,
_variant_index: usize, variant_index: usize,
_variant: &'static str) -> Result<(), Self::Error> { variant: &'static 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: &'static 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: &'static str,
variant_index: usize, variant_index: usize,
variant: &'static str, variant: &'static 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 sequence whose length will be known at
/// /// deserialization time. This call must be followed by zero or more calls
/// By default this serializes a tuple as a sequence. /// to `serialize_seq_elt`, then a call to `serialize_seq_end`. A reasonable
#[inline] /// implementation would be to forward to `serialize_seq`.
fn serialize_tuple(&mut self, len: usize) -> Result<Self::SeqState, Self::Error> fn serialize_seq_fixed_size(
{ &mut self,
self.serialize_seq(Some(len)) size: usize,
} ) -> Result<Self::SeqState, Self::Error>;
/// Serializes a tuple element. /// 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, tuples are serialized as a sequence. /// reasonable implementation would be to forward to `serialize_seq`.
#[inline] fn serialize_tuple(
fn serialize_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error> &mut self,
where T: Serialize { len: usize,
self.serialize_seq_elt(value) ) -> Result<Self::TupleState, Self::Error>;
}
/// Finishes serialization of a tuple. /// 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_end(&mut self, len: usize, state: Self::SeqState) -> Result<(), Self::Error> { state: &mut Self::TupleState,
self.serialize_seq_end(Some(len), state) value: T,
} ) -> Result<(), Self::Error>;
/// Serializes a fixed-size array. /// Finishes serializing a tuple.
/// fn serialize_tuple_end(
/// By default this serializes an array as a sequence. &mut self,
#[inline] state: Self::TupleState,
fn serialize_fixed_size_array(&mut self, size: usize) -> Result<Self::SeqState, Self::Error> ) -> Result<(), Self::Error>;
{
self.serialize_seq(Some(size))
}
/// Serializes a tuple struct. /// 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, tuple structs are serialized as a tuple. /// `serialize_tuple_struct_end`. A reasonable implementation would be to
#[inline] /// forward to `serialize_tuple`.
fn serialize_tuple_struct(&mut self, fn serialize_tuple_struct(
_name: &'static str, &mut self,
len: usize, name: &'static str,
) -> Result<Self::SeqState, Self::Error> len: usize,
{ ) -> Result<Self::TupleStructState, Self::Error>;
self.serialize_tuple(len)
}
/// Serializes a tuple struct element. /// Serializes a tuple struct element. Must have previously called
/// /// `serialize_tuple_struct`.
/// By default, tuple struct elements are serialized as a tuple element. fn serialize_tuple_struct_elt<T: Serialize>(
#[inline] &mut self,
fn serialize_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Self::Error> state: &mut Self::TupleStructState,
where T: Serialize value: T,
{ ) -> Result<(), Self::Error>;
self.serialize_tuple_elt(value)
}
/// Finishes serialization of a tuple struct. /// Finishes serializing a tuple struct.
/// fn serialize_tuple_struct_end(
/// By default, tuple structs are serialized as a sequence. &mut self,
#[inline] state: Self::TupleStructState,
fn serialize_tuple_struct_end(&mut self, ) -> Result<(), Self::Error>;
_name: &'static str,
len: usize,
state: Self::SeqState,
) -> Result<(), Self::Error> {
self.serialize_tuple_end(len, state)
}
/// Serializes a tuple variant. /// 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 variants are serialized as a tuple struct. /// `serialize_tuple_variant_end`. A reasonable implementation would be to
#[inline] /// forward to `serialize_tuple_struct`.
fn serialize_tuple_variant(&mut self, fn serialize_tuple_variant(
_name: &'static str, &mut self,
_variant_index: usize, name: &'static str,
variant: &'static str, variant_index: usize,
len: usize, variant: &'static str,
) -> Result<Self::SeqState, Self::Error> len: usize,
{ ) -> Result<Self::TupleVariantState, Self::Error>;
self.serialize_tuple_struct(variant, len)
}
/// Serializes a tuple variant element. /// Serializes a tuple variant element. Must have previously called
/// /// `serialize_tuple_variant`.
/// By default, tuple variants are serialized as tuples. fn serialize_tuple_variant_elt<T: Serialize>(
#[inline] &mut self,
fn serialize_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Self::Error> state: &mut Self::TupleVariantState,
where T: Serialize value: T,
{ ) -> Result<(), Self::Error>;
self.serialize_tuple_struct_elt(value)
}
/// Finishes serialization of a tuple variant. /// 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_end(&mut self, ) -> Result<(), Self::Error>;
_name: &'static str,
_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. /// Begins to serialize a map. This call must be followed by zero or more
fn serialize_map(&mut self, len: Option<usize>) -> Result<Self::MapState, Self::Error>; /// calls to `serialize_map_elt`, then a call to `serialize_map_end`.
fn serialize_map(
&mut self,
len: Option<usize>,
) -> Result<Self::MapState, Self::Error>;
/// Serialize a map element /// Serialize a map element. Must have previously called `serialize_map`.
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize; fn serialize_map_elt<K: Serialize, V: Serialize>(
&mut self,
state: &mut Self::MapState,
key: K,
value: V,
) -> Result<(), Self::Error>;
/// Finishes serializing a map /// Finishes serializing a map.
fn serialize_map_end(&mut self, len: Option<usize>, state: Self::MapState) -> Result<(), Self::Error>; fn serialize_map_end(
&mut self,
state: Self::MapState,
) -> Result<(), Self::Error>;
/// Serializes a struct. /// Begins to serialize a struct. This call must be followed by zero or more
/// /// calls to `serialize_struct_elt`, then a call to `serialize_struct_end`.
/// By default, structs are serialized as a map with the field name as the key. fn serialize_struct(
#[inline] &mut self,
fn serialize_struct(&mut self, name: &'static str,
_name: &'static str, len: usize,
len: usize, ) -> Result<Self::StructState, Self::Error>;
) -> Result<Self::MapState, Self::Error>
{
self.serialize_map(Some(len))
}
/// Serialize a struct field /// 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>(
fn serialize_struct_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize { &mut self,
self.serialize_map_elt(key, value) state: &mut Self::StructState,
} key: &'static str,
value: V,
) -> Result<(), Self::Error>;
/// Finishes serializing a struct /// 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_end(&mut self, state: Self::StructState,
_name: &'static str, ) -> Result<(), Self::Error>;
len: usize,
state: Self::MapState,
) -> Result<(), Self::Error> {
self.serialize_map_end(Some(len), state)
}
/// Serializes a struct variant. /// 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, struct variants are serialized as a struct. /// `serialize_struct_variant_end`.
#[inline] fn serialize_struct_variant(
fn serialize_struct_variant(&mut self, &mut self,
_name: &'static str, name: &'static str,
_variant_index: usize, variant_index: usize,
variant: &'static str, variant: &'static str,
len: usize, len: usize,
) -> Result<Self::MapState, Self::Error> ) -> Result<Self::StructVariantState, Self::Error>;
{
self.serialize_struct(variant, len)
}
/// Serialize a struct variant element /// Serialize a struct variant element. Must have previously called
/// /// `serialize_struct_variant`.
/// By default, structs are serialized as a map with the field name as the key. fn serialize_struct_variant_elt<V: Serialize>(
fn serialize_struct_variant_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize { &mut self,
self.serialize_struct_elt(key, value) state: &mut Self::StructVariantState,
} key: &'static str,
value: V,
) -> Result<(), Self::Error>;
/// Finishes serializing a struct variant /// 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_end(&mut self, state: Self::StructVariantState,
_name: &'static str, ) -> Result<(), Self::Error>;
_variant_index: usize,
variant: &'static str,
len: usize,
state: Self::MapState,
) -> Result<(), Self::Error> {
self.serialize_struct_end(variant, len, state)
}
} }