diff --git a/serde/src/bytes.rs b/serde/src/bytes.rs index 5fa995fb..fd96cea1 100644 --- a/serde/src/bytes.rs +++ b/serde/src/bytes.rs @@ -65,7 +65,7 @@ impl<'a> ops::Deref for Bytes<'a> { impl<'a> ser::Serialize for Bytes<'a> { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { serializer.serialize_bytes(self.bytes) @@ -168,7 +168,7 @@ mod bytebuf { } impl ser::Serialize for ByteBuf { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer { serializer.serialize_bytes(self) diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 4be35fc3..3de53ebe 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -69,6 +69,10 @@ use core::nonzero::{NonZero, Zeroable}; use super::{ Error, Serialize, + SerializeMap, + SerializeSeq, + SerializeStruct, + SerializeTuple, Serializer, }; @@ -81,7 +85,7 @@ macro_rules! impl_visit { ($ty:ty, $method:ident) => { impl Serialize for $ty { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.$method(*self) @@ -109,7 +113,7 @@ impl_visit!(char, serialize_char); impl Serialize for str { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(self) @@ -119,7 +123,7 @@ impl Serialize for str { #[cfg(any(feature = "std", feature = "collections"))] impl Serialize for String { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (&self[..]).serialize(serializer) @@ -128,9 +132,11 @@ impl Serialize for String { /////////////////////////////////////////////////////////////////////////////// -impl Serialize for Option where T: Serialize { +impl Serialize for Option + where T: Serialize +{ #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { @@ -144,7 +150,7 @@ impl Serialize for Option where T: Serialize { impl Serialize for PhantomData { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_unit_struct("PhantomData") @@ -158,14 +164,14 @@ impl Serialize for [T] where T: Serialize, { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut state = try!(serializer.serialize_seq(Some(self.len()))); + let mut seq = try!(serializer.serialize_seq(Some(self.len()))); for e in self { - try!(serializer.serialize_seq_elt(&mut state, e)); + try!(seq.serialize_elem(e)); } - serializer.serialize_seq_end(state) + seq.serialize_end() } } @@ -175,14 +181,14 @@ macro_rules! array_impls { ($len:expr) => { impl Serialize for [T; $len] where T: Serialize { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut state = try!(serializer.serialize_seq_fixed_size($len)); + let mut seq = try!(serializer.serialize_seq_fixed_size($len)); for e in self { - try!(serializer.serialize_seq_elt(&mut state, e)); + try!(seq.serialize_elem(e)); } - serializer.serialize_seq_end(state) + seq.serialize_end() } } } @@ -229,7 +235,7 @@ impl<'a, I> Serialize for Iterator where I: IntoIterator, ::Item: Serialize { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { // FIXME: use specialization to prevent invalidating the object in case of clonable iterators? @@ -241,11 +247,11 @@ impl<'a, I> Serialize for Iterator (lo, Some(hi)) if lo == hi => Some(lo), _ => None, }; - let mut state = try!(serializer.serialize_seq(size)); + let mut seq = try!(serializer.serialize_seq(size)); for e in iter { - try!(serializer.serialize_seq_elt(&mut state, e)); + try!(seq.serialize_elem(e)); } - serializer.serialize_seq_end(state) + seq.serialize_end() } } @@ -254,14 +260,14 @@ impl<'a, I> Serialize for Iterator macro_rules! serialize_seq { () => { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut state = try!(serializer.serialize_seq(Some(self.len()))); + let mut seq = try!(serializer.serialize_seq(Some(self.len()))); for e in self { - try!(serializer.serialize_seq_elt(&mut state, e)); + try!(seq.serialize_elem(e)); } - serializer.serialize_seq_end(state) + seq.serialize_end() } } } @@ -303,12 +309,16 @@ impl Serialize for LinkedList } #[cfg(any(feature = "std", feature = "collections"))] -impl Serialize for Vec where T: Serialize { +impl Serialize for Vec + where T: Serialize +{ serialize_seq!(); } #[cfg(any(feature = "std", feature = "collections"))] -impl Serialize for VecDeque where T: Serialize { +impl Serialize for VecDeque + where T: Serialize +{ serialize_seq!(); } @@ -318,15 +328,15 @@ impl Serialize for ops::Range for<'a> &'a A: ops::Add<&'a A, Output = A>, { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { let len = iter::Step::steps_between(&self.start, &self.end, &A::one()); - let mut state = try!(serializer.serialize_seq(len)); + let mut seq = try!(serializer.serialize_seq(len)); for e in self.clone() { - try!(serializer.serialize_seq_elt(&mut state, e)); + try!(seq.serialize_elem(e)); } - serializer.serialize_seq_end(state) + seq.serialize_end() } } @@ -334,7 +344,7 @@ impl Serialize for ops::Range impl Serialize for () { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_unit() @@ -354,14 +364,14 @@ macro_rules! tuple_impls { where $($T: Serialize),+ { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut state = try!(serializer.serialize_tuple($len)); + let mut tuple = try!(serializer.serialize_tuple($len)); $( - try!(serializer.serialize_tuple_elt(&mut state, &self.$idx)); + try!(tuple.serialize_elem(&self.$idx)); )+ - serializer.serialize_tuple_end(state) + tuple.serialize_end() } } )+ @@ -544,15 +554,15 @@ tuple_impls! { macro_rules! serialize_map { () => { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { - let mut state = try!(serializer.serialize_map(Some(self.len()))); + let mut map = try!(serializer.serialize_map(Some(self.len()))); for (k, v) in self { - try!(serializer.serialize_map_key(&mut state, k)); - try!(serializer.serialize_map_value(&mut state, v)); + try!(map.serialize_key(k)); + try!(map.serialize_value(v)); } - serializer.serialize_map_end(state) + map.serialize_end() } } } @@ -578,7 +588,7 @@ impl Serialize for HashMap impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -587,7 +597,7 @@ impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize { impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -595,9 +605,11 @@ impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize { } #[cfg(any(feature = "std", feature = "alloc"))] -impl Serialize for Box where T: Serialize { +impl Serialize for Box + where T: Serialize +{ #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -605,9 +617,11 @@ impl Serialize for Box where T: Serialize { } #[cfg(any(feature = "std", feature = "alloc"))] -impl Serialize for Rc where T: Serialize, { +impl Serialize for Rc + where T: Serialize +{ #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -615,9 +629,11 @@ impl Serialize for Rc where T: Serialize, { } #[cfg(any(feature = "std", feature = "alloc"))] -impl Serialize for Arc where T: Serialize, { +impl Serialize for Arc + where T: Serialize +{ #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -625,9 +641,11 @@ impl Serialize for Arc where T: Serialize, { } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, { +impl<'a, T: ?Sized> Serialize for Cow<'a, T> + where T: Serialize + ToOwned +{ #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { (**self).serialize(serializer) @@ -636,8 +654,13 @@ impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, { /////////////////////////////////////////////////////////////////////////////// -impl Serialize for Result where T: Serialize, E: Serialize { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer { +impl Serialize for Result + where T: Serialize, + E: Serialize +{ + fn serialize(&self, serializer: S) -> Result + where S: Serializer + { match *self { Result::Ok(ref value) => { serializer.serialize_newtype_variant("Result", 0, "Ok", value) @@ -653,13 +676,13 @@ impl Serialize for Result where T: Serialize, E: Serialize { #[cfg(feature = "std")] impl Serialize for Duration { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut state = try!(serializer.serialize_struct("Duration", 2)); - try!(serializer.serialize_struct_elt(&mut state, "secs", self.as_secs())); - try!(serializer.serialize_struct_elt(&mut state, "nanos", self.subsec_nanos())); - serializer.serialize_struct_end(state) + try!(state.serialize_field("secs", self.as_secs())); + try!(state.serialize_field("nanos", self.subsec_nanos())); + state.serialize_end() } } @@ -667,7 +690,7 @@ impl Serialize for Duration { #[cfg(feature = "std")] impl Serialize for net::IpAddr { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.to_string().serialize(serializer) @@ -676,7 +699,7 @@ impl Serialize for net::IpAddr { #[cfg(feature = "std")] impl Serialize for net::Ipv4Addr { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.to_string().serialize(serializer) @@ -685,7 +708,7 @@ impl Serialize for net::Ipv4Addr { #[cfg(feature = "std")] impl Serialize for net::Ipv6Addr { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.to_string().serialize(serializer) @@ -696,7 +719,7 @@ impl Serialize for net::Ipv6Addr { #[cfg(feature = "std")] impl Serialize for net::SocketAddr { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { @@ -708,7 +731,7 @@ impl Serialize for net::SocketAddr { #[cfg(feature = "std")] impl Serialize for net::SocketAddrV4 { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.to_string().serialize(serializer) @@ -717,7 +740,7 @@ impl Serialize for net::SocketAddrV4 { #[cfg(feature = "std")] impl Serialize for net::SocketAddrV6 { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.to_string().serialize(serializer) @@ -728,7 +751,7 @@ impl Serialize for net::SocketAddrV6 { #[cfg(feature = "std")] impl Serialize for path::Path { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.to_str() { @@ -740,7 +763,7 @@ impl Serialize for path::Path { #[cfg(feature = "std")] impl Serialize for path::PathBuf { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.as_path().serialize(serializer) @@ -748,8 +771,12 @@ impl Serialize for path::PathBuf { } #[cfg(feature = "unstable")] -impl Serialize for NonZero where T: Serialize + Zeroable { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer { +impl Serialize for NonZero + where T: Serialize + Zeroable +{ + fn serialize(&self, serializer: S) -> Result + where S: Serializer + { (**self).serialize(serializer) } } diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 2c359b23..aa19f362 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -49,7 +49,7 @@ pub trait Error: Sized + error::Error { /// A trait that describes a type that can be serialized by a `Serializer`. pub trait Serialize { /// Serializes this value into this serializer. - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: Serializer; } @@ -77,345 +77,357 @@ pub trait Serialize { /// state object, as it is expected that the user will not modify it. Due to the generic nature /// of the `Serialize` impls, modifying the object is impossible on stable Rust. pub trait Serializer { - /// The error type that can be returned if some error occurs during serialization. + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `Serializer` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. type Error: Error; - /// 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; - /// A state object that is initialized by `serialize_tuple`, passed to - /// `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; - /// 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; + /// Type returned from `serialize_seq` and `serialize_seq_fixed_size` for + /// serializing the content of the sequence. + type SerializeSeq: SerializeSeq; + + /// Type returned from `serialize_tuple` for serializing the content of the + /// tuple. + type SerializeTuple: SerializeTuple; + + /// Type returned from `serialize_tuple_struct` for serializing the content + /// of the tuple struct. + type SerializeTupleStruct: SerializeTupleStruct; + + /// Type returned from `serialize_tuple_variant` for serializing the content + /// of the tuple variant. + type SerializeTupleVariant: SerializeTupleVariant; + + /// Type returned from `serialize_map` for serializing the content of the + /// map. + type SerializeMap: SerializeMap; + + /// Type returned from `serialize_struct` for serializing the content of the + /// struct. + type SerializeStruct: SerializeStruct; + + /// Type returned from `serialize_struct_variant` for serializing the + /// content of the struct variant. + type SerializeStructVariant: SerializeStructVariant; /// Serializes a `bool` value. - fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>; + fn serialize_bool(self, v: bool) -> Result; /// Serializes an `isize` value. If the format does not differentiate /// between `isize` and `i64`, a reasonable implementation would be to cast /// the value to `i64` and forward to `serialize_i64`. - fn serialize_isize(&mut self, v: isize) -> Result<(), Self::Error>; + fn serialize_isize(self, v: isize) -> Result; /// Serializes an `i8` value. If the format does not differentiate between /// `i8` and `i64`, a reasonable implementation would be to cast the value /// to `i64` and forward to `serialize_i64`. - fn serialize_i8(&mut self, v: i8) -> Result<(), Self::Error>; + fn serialize_i8(self, v: i8) -> Result; /// Serializes an `i16` value. If the format does not differentiate between /// `i16` and `i64`, a reasonable implementation would be to cast the value /// to `i64` and forward to `serialize_i64`. - fn serialize_i16(&mut self, v: i16) -> Result<(), Self::Error>; + fn serialize_i16(self, v: i16) -> Result; /// Serializes an `i32` value. If the format does not differentiate between /// `i32` and `i64`, a reasonable implementation would be to cast the value /// to `i64` and forward to `serialize_i64`. - fn serialize_i32(&mut self, v: i32) -> Result<(), Self::Error>; + fn serialize_i32(self, v: i32) -> Result; /// Serializes an `i64` value. - fn serialize_i64(&mut self, v: i64) -> Result<(), Self::Error>; + fn serialize_i64(self, v: i64) -> Result; /// Serializes a `usize` value. If the format does not differentiate between /// `usize` and `u64`, a reasonable implementation would be to cast the /// value to `u64` and forward to `serialize_u64`. - fn serialize_usize(&mut self, v: usize) -> Result<(), Self::Error>; + fn serialize_usize(self, v: usize) -> Result; /// Serializes a `u8` value. If the format does not differentiate between /// `u8` and `u64`, a reasonable implementation would be to cast the value /// to `u64` and forward to `serialize_u64`. - fn serialize_u8(&mut self, v: u8) -> Result<(), Self::Error>; + fn serialize_u8(self, v: u8) -> Result; /// Serializes a `u16` value. If the format does not differentiate between /// `u16` and `u64`, a reasonable implementation would be to cast the value /// to `u64` and forward to `serialize_u64`. - fn serialize_u16(&mut self, v: u16) -> Result<(), Self::Error>; + fn serialize_u16(self, v: u16) -> Result; /// Serializes a `u32` value. If the format does not differentiate between /// `u32` and `u64`, a reasonable implementation would be to cast the value /// to `u64` and forward to `serialize_u64`. - fn serialize_u32(&mut self, v: u32) -> Result<(), Self::Error>; + fn serialize_u32(self, v: u32) -> Result; /// `Serializes a `u64` value. - fn serialize_u64(&mut self, v: u64) -> Result<(), Self::Error>; + fn serialize_u64(self, v: u64) -> Result; /// Serializes an `f32` value. If the format does not differentiate between /// `f32` and `f64`, a reasonable implementation would be to cast the value /// to `f64` and forward to `serialize_f64`. - fn serialize_f32(&mut self, v: f32) -> Result<(), Self::Error>; + fn serialize_f32(self, v: f32) -> Result; /// Serializes an `f64` value. - fn serialize_f64(&mut self, v: f64) -> Result<(), Self::Error>; + fn serialize_f64(self, v: f64) -> Result; /// Serializes a character. If the format does not support characters, /// it is reasonable to serialize it as a single element `str` or a `u32`. - fn serialize_char(&mut self, v: char) -> Result<(), Self::Error>; + fn serialize_char(self, v: char) -> Result; /// Serializes a `&str`. - fn serialize_str(&mut self, value: &str) -> Result<(), Self::Error>; + fn serialize_str(self, value: &str) -> Result; /// Enables serializers to serialize byte slices more compactly or more /// efficiently than other types of slices. If no efficient implementation /// is available, a reasonable implementation would be to forward to /// `serialize_seq`. If forwarded, the implementation looks usually just like this: /// ```rust - /// let mut state = try!(self.serialize_seq(value)); + /// let mut seq = self.serialize_seq(Some(value.len()))?; /// for b in value { - /// try!(self.serialize_seq_elt(&mut state, b)); + /// seq.serialize_elem(b)?; /// } - /// self.serialize_seq_end(state) + /// seq.serialize_end() /// ``` - fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>; + fn serialize_bytes(self, value: &[u8]) -> Result; /// Serializes a `()` value. It's reasonable to just not serialize anything. - fn serialize_unit(&mut self) -> Result<(), Self::Error>; + fn serialize_unit(self) -> Result; /// Serializes a unit struct value. A reasonable implementation would be to /// forward to `serialize_unit`. fn serialize_unit_struct( - &mut self, + self, name: &'static str, - ) -> Result<(), Self::Error>; + ) -> Result; /// Serializes a unit variant, otherwise known as a variant with no /// arguments. A reasonable implementation would be to forward to /// `serialize_unit`. fn serialize_unit_variant( - &mut self, + self, name: &'static str, variant_index: usize, variant: &'static str, - ) -> Result<(), Self::Error>; + ) -> Result; /// Allows a tuple struct with a single element, also known as a newtype /// struct, to be more efficiently serialized than a tuple struct with /// multiple items. A reasonable implementation would be to forward to /// `serialize_tuple_struct` or to just serialize the inner value without wrapping. fn serialize_newtype_struct( - &mut self, + self, name: &'static str, value: T, - ) -> Result<(), Self::Error>; + ) -> Result; /// Allows a variant with a single item to be more efficiently serialized /// than a variant with multiple items. A reasonable implementation would be /// to forward to `serialize_tuple_variant`. fn serialize_newtype_variant( - &mut self, + self, name: &'static str, variant_index: usize, variant: &'static str, value: T, - ) -> Result<(), Self::Error>; + ) -> Result; /// Serializes a `None` value. - fn serialize_none(&mut self) -> Result<(), Self::Error>; + fn serialize_none(self) -> Result; /// Serializes a `Some(...)` value. fn serialize_some( - &mut self, + self, value: T, - ) -> Result<(), Self::Error>; + ) -> Result; /// 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`. fn serialize_seq( - &mut self, + self, len: Option, - ) -> Result; - - /// Serializes a sequence element. Must have previously called - /// `serialize_seq`. - fn serialize_seq_elt( - &mut self, - state: &mut Self::SeqState, - value: T, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a sequence. - fn serialize_seq_end( - &mut self, - state: Self::SeqState, - ) -> Result<(), Self::Error>; + ) -> Result; /// Begins to serialize a sequence whose length will be known at /// deserialization time. This call must be followed by zero or more calls /// to `serialize_seq_elt`, then a call to `serialize_seq_end`. A reasonable /// implementation would be to forward to `serialize_seq`. fn serialize_seq_fixed_size( - &mut self, + self, size: usize, - ) -> Result; + ) -> Result; /// 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 /// reasonable implementation would be to forward to `serialize_seq`. fn serialize_tuple( - &mut self, + self, len: usize, - ) -> Result; - - /// Serializes a tuple element. Must have previously called - /// `serialize_tuple`. - fn serialize_tuple_elt( - &mut self, - state: &mut Self::TupleState, - value: T, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a tuple. - fn serialize_tuple_end( - &mut self, - state: Self::TupleState, - ) -> Result<(), Self::Error>; + ) -> Result; /// 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 /// `serialize_tuple_struct_end`. A reasonable implementation would be to /// forward to `serialize_tuple`. fn serialize_tuple_struct( - &mut self, + self, name: &'static str, len: usize, - ) -> Result; - - /// Serializes a tuple struct element. Must have previously called - /// `serialize_tuple_struct`. - fn serialize_tuple_struct_elt( - &mut self, - state: &mut Self::TupleStructState, - value: T, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a tuple struct. - fn serialize_tuple_struct_end( - &mut self, - state: Self::TupleStructState, - ) -> Result<(), Self::Error>; + ) -> Result; /// 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 /// `serialize_tuple_variant_end`. A reasonable implementation would be to /// forward to `serialize_tuple_struct`. fn serialize_tuple_variant( - &mut self, + self, name: &'static str, variant_index: usize, variant: &'static str, len: usize, - ) -> Result; - - /// Serializes a tuple variant element. Must have previously called - /// `serialize_tuple_variant`. - fn serialize_tuple_variant_elt( - &mut self, - state: &mut Self::TupleVariantState, - value: T, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a tuple variant. - fn serialize_tuple_variant_end( - &mut self, - state: Self::TupleVariantState, - ) -> Result<(), Self::Error>; + ) -> Result; /// Begins to serialize a map. This call must be followed by zero or more /// calls to `serialize_map_key` and `serialize_map_value`, then a call to /// `serialize_map_end`. fn serialize_map( - &mut self, + self, len: Option, - ) -> Result; - - /// Serialize a map key. Must have previously called `serialize_map`. - fn serialize_map_key( - &mut self, - state: &mut Self::MapState, - key: T - ) -> Result<(), Self::Error>; - - /// Serialize a map value. Must have previously called `serialize_map`. - fn serialize_map_value( - &mut self, - state: &mut Self::MapState, - value: T - ) -> Result<(), Self::Error>; - - /// Finishes serializing a map. - fn serialize_map_end( - &mut self, - state: Self::MapState, - ) -> Result<(), Self::Error>; + ) -> Result; /// 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`. fn serialize_struct( - &mut self, + self, name: &'static str, len: usize, - ) -> Result; - - /// Serializes a struct field. Must have previously called - /// `serialize_struct`. - fn serialize_struct_elt( - &mut self, - state: &mut Self::StructState, - key: &'static str, - value: V, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a struct. - fn serialize_struct_end( - &mut self, - state: Self::StructState, - ) -> Result<(), Self::Error>; + ) -> Result; /// 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 /// `serialize_struct_variant_end`. fn serialize_struct_variant( - &mut self, + self, name: &'static str, variant_index: usize, variant: &'static str, len: usize, - ) -> Result; - - /// Serialize a struct variant element. Must have previously called - /// `serialize_struct_variant`. - fn serialize_struct_variant_elt( - &mut self, - state: &mut Self::StructVariantState, - key: &'static str, - value: V, - ) -> Result<(), Self::Error>; - - /// Finishes serializing a struct variant. - fn serialize_struct_variant_end( - &mut self, - state: Self::StructVariantState, - ) -> Result<(), Self::Error>; + ) -> Result; } +/// Returned from `Serializer::serialize_seq` and +/// `Serializer::serialize_seq_fixed_size`. +pub trait SerializeSeq { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeSeq` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serializes a sequence element. + fn serialize_elem(&mut self, value: T) -> Result<(), Self::Error>; + + /// Finishes serializing a sequence. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_tuple`. +pub trait SerializeTuple { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeTuple` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serializes a tuple element. + fn serialize_elem(&mut self, value: T) -> Result<(), Self::Error>; + + /// Finishes serializing a tuple. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_tuple_struct`. +pub trait SerializeTupleStruct { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeTupleStruct` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serializes a tuple struct element. + fn serialize_elem(&mut self, value: T) -> Result<(), Self::Error>; + + /// Finishes serializing a tuple struct. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_tuple_variant`. +pub trait SerializeTupleVariant { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeTupleVariant` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serializes a tuple variant element. + fn serialize_elem(&mut self, value: T) -> Result<(), Self::Error>; + + /// Finishes serializing a tuple variant. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_map`. +pub trait SerializeMap { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeMap` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serialize a map key. + fn serialize_key(&mut self, key: T) -> Result<(), Self::Error>; + + /// Serialize a map value. + fn serialize_value(&mut self, value: T) -> Result<(), Self::Error>; + + /// Finishes serializing a map. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_struct`. +pub trait SerializeStruct { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeStruct` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serializes a struct field. + fn serialize_field(&mut self, key: &'static str, value: V) -> Result<(), Self::Error>; + + /// Finishes serializing a struct. + fn serialize_end(self) -> Result; +} + +/// Returned from `Serializer::serialize_struct_variant`. +pub trait SerializeStructVariant { + /// Trickery to enforce correct use of the `Serialize` trait. Every + /// `SerializeStructVariant` should set `Ok = ()`. + type Ok; + + /// The error type when some error occurs during serialization. + type Error: Error; + + /// Serialize a struct variant element. + fn serialize_field(&mut self, key: &'static str, value: V) -> Result<(), Self::Error>; + + /// Finishes serializing a struct variant. + fn serialize_end(self) -> Result; +} /// A wrapper type for iterators that implements `Serialize` for iterators whose items implement /// `Serialize`. Don't use multiple times. Create new versions of this with the `iterator` function