diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index d0ac49bc..cd8e5964 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -64,8 +64,8 @@ use super::{ Error, Serialize, Serializer, - SeqVisitor, - MapVisitor, + SeqSerializer, + MapSerializer, }; /////////////////////////////////////////////////////////////////////////////// @@ -133,26 +133,6 @@ impl Serialize for Option where T: Serialize { } } -impl SeqVisitor for Option where T: Serialize { - #[inline] - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer, - { - match self.take() { - Some(value) => { - try!(serializer.serialize_seq_elt(value)); - Ok(Some(())) - } - None => Ok(None), - } - } - - #[inline] - fn len(&self) -> Option { - Some(if self.is_some() { 1 } else { 0 }) - } -} - /////////////////////////////////////////////////////////////////////////////// impl Serialize for PhantomData { @@ -164,69 +144,6 @@ impl Serialize for PhantomData { } } -/////////////////////////////////////////////////////////////////////////////// - -/// A `serde::Visitor` for sequence iterators. -/// -/// # Examples -/// -/// ``` -/// use serde::{Serialize, Serializer}; -/// use serde::ser::impls::SeqIteratorVisitor; -/// -/// struct Seq(Vec); -/// -/// impl Serialize for Seq { -/// fn serialize(&self, ser: &mut S) -> Result<(), S::Error> -/// where S: Serializer, -/// { -/// ser.serialize_seq(SeqIteratorVisitor::new( -/// self.0.iter(), -/// Some(self.0.len()), -/// )) -/// } -/// } -/// ``` -pub struct SeqIteratorVisitor { - iter: Iter, - len: Option, -} - -impl SeqIteratorVisitor - where Iter: Iterator -{ - /// Construct a new `SeqIteratorVisitor`. - #[inline] - pub fn new(iter: Iter, len: Option) -> SeqIteratorVisitor { - SeqIteratorVisitor { - iter: iter, - len: len, - } - } -} - -impl SeqVisitor for SeqIteratorVisitor - where T: Serialize, - Iter: Iterator, -{ - #[inline] - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer, - { - match self.iter.next() { - Some(value) => { - try!(serializer.serialize_seq_elt(value)); - Ok(Some(())) - } - None => Ok(None), - } - } - - #[inline] - fn len(&self) -> Option { - self.len - } -} /////////////////////////////////////////////////////////////////////////////// @@ -237,7 +154,12 @@ impl Serialize for [T] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -250,8 +172,11 @@ macro_rules! array_impls { fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - let visitor = SeqIteratorVisitor::new(self.iter(), Some($len)); - serializer.serialize_fixed_size_array(visitor) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } } @@ -301,7 +226,11 @@ impl Serialize for BinaryHeap fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -313,7 +242,11 @@ impl Serialize for BTreeSet fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -325,7 +258,10 @@ impl Serialize for EnumSet fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } } } @@ -338,7 +274,11 @@ impl Serialize for HashSet fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -350,7 +290,11 @@ impl Serialize for LinkedList fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -361,10 +305,13 @@ impl Serialize for ops::Range { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> - where S: Serializer, + where S: Serialize, { - let len = iter::Step::steps_between(&self.start, &self.end, &A::one()); - serializer.serialize_seq(SeqIteratorVisitor::new(self.clone(), len)) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -384,7 +331,11 @@ impl Serialize for VecDeque where T: Serialize { fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut seq_serializer = try!(serializer.serialize_seq()); + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } + Ok(()) } } @@ -406,6 +357,7 @@ macro_rules! e { ($e:expr) => { $e } } +/* TODO: FIX tuple magic macro_rules! tuple_impls { ($( $TupleVisitor:ident ($len:expr, $($T:ident),+) { @@ -413,51 +365,18 @@ macro_rules! tuple_impls { } )+) => { $( - /// A tuple visitor. - pub struct $TupleVisitor<'a, $($T: 'a),+> { - tuple: &'a ($($T,)+), - state: u8, - } - - impl<'a, $($T: 'a),+> $TupleVisitor<'a, $($T),+> { - /// Construct a new, empty `TupleVisitor`. - pub fn new(tuple: &'a ($($T,)+)) -> $TupleVisitor<'a, $($T),+> { - $TupleVisitor { - tuple: tuple, - state: 0, - } - } - } - - impl<'a, $($T),+> SeqVisitor for $TupleVisitor<'a, $($T),+> - where $($T: Serialize),+ - { - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer, - { - match self.state { - $( - $state => { - self.state += 1; - Ok(Some(try!(serializer.serialize_tuple_elt(&e!(self.tuple.$idx))))) - } - )+ - _ => { - Ok(None) - } - } - } - - fn len(&self) -> Option { - Some($len) - } - } - impl<$($T),+> Serialize for ($($T,)+) where $($T: Serialize),+ { #[inline] - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> { + fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + where S: Serializer, + SS : SeqSerializer, + { + + for e in self.iter() { + try!(seq_serializer.serialize_elt(e)); + } serializer.serialize_tuple($TupleVisitor::new(self)) } } @@ -635,72 +554,7 @@ tuple_impls! { 15 => 15, } } - -/////////////////////////////////////////////////////////////////////////////// - -/// A `serde::Visitor` for (key, value) map iterators. -/// -/// # Examples -/// -/// ``` -/// use std::collections::HashMap; -/// use serde::{Serialize, Serializer}; -/// use serde::ser::impls::MapIteratorVisitor; -/// -/// struct Map(HashMap); -/// -/// impl Serialize for Map { -/// fn serialize(&self, ser: &mut S) -> Result<(), S::Error> -/// where S: Serializer, -/// { -/// ser.serialize_map(MapIteratorVisitor::new( -/// self.0.iter(), -/// Some(self.0.len()), -/// )) -/// } -/// } -/// ``` -pub struct MapIteratorVisitor { - iter: Iter, - len: Option, -} - -impl MapIteratorVisitor - where Iter: Iterator -{ - /// Construct a new `MapIteratorVisitor`. - #[inline] - pub fn new(iter: Iter, len: Option) -> MapIteratorVisitor { - MapIteratorVisitor { - iter: iter, - len: len, - } - } -} - -impl MapVisitor for MapIteratorVisitor - where K: Serialize, - V: Serialize, - I: Iterator, -{ - #[inline] - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer, - { - match self.iter.next() { - Some((key, value)) => { - try!(serializer.serialize_map_elt(key, value)); - Ok(Some(())) - } - None => Ok(None) - } - } - - #[inline] - fn len(&self) -> Option { - self.len - } -} +*/ /////////////////////////////////////////////////////////////////////////////// @@ -713,7 +567,11 @@ impl Serialize for BTreeMap fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut map_serializer = try!(serializer.serialize_map()); + for (k, v) in self.iter() { + try!(map_serializer.serialize_elt(k, v)); + } + Ok(()) } } @@ -727,7 +585,11 @@ impl Serialize for HashMap fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len()))) + let mut map_serializer = try!(serializer.serialize_map()); + for (k, v) in self.iter() { + try!(map_serializer.serialize_elt(k, v)); + } + Ok(()) } } diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 6040ea11..c42efe6e 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -38,6 +38,27 @@ pub trait Serialize { where S: Serializer; } +/// A trait that described a type that can serialize elements of a Map +pub trait MapSerializer : Sized { + /// The error type that can be returned if some error occurs during serialization. + type Error: Error; + + /// Serializes an element of a map (key-value pair). + fn serialize_elt(&mut self, key: K, value: V) -> Result<(), Self::Error> + where K: Serialize, + V: Serialize; +} + +/// A trait that described a type that can serialize elements of a Sequence +pub trait SeqSerializer : Sized { + /// The error type that can be returned if some error occurs during serialization. + type Error: Error; + + /// Serializes an element of a sequence. + fn serialize_elt(&mut self, value: T) -> Result<(), Self::Error> + where T: Serialize; +} + /////////////////////////////////////////////////////////////////////////////// /// A trait that describes a type that can serialize a stream of values into the underlying format. @@ -45,6 +66,11 @@ pub trait Serializer { /// The error type that can be returned if some error occurs during serialization. type Error: Error; + /// A sub-type used to serialize sequences + type SeqSerializer : SeqSerializer; + /// A sub-type used to serialize maps + type MapSerializer : MapSerializer; + /// Serializes a `bool` value. fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>; @@ -136,7 +162,11 @@ pub trait Serializer { /// byte slices separately from generic arrays. By default it serializes as a regular array. #[inline] fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> { - self.serialize_seq(impls::SeqIteratorVisitor::new(value.iter(), Some(value.len()))) + let mut seq_serializer = try!(self.serialize_seq()); + for b in value.iter() { + try!(seq_serializer.serialize_elt(b)); + } + Ok(()) } /// Serializes a `()` value. @@ -170,7 +200,7 @@ pub trait Serializer { value: T) -> Result<(), Self::Error> where T: Serialize, { - self.serialize_tuple_struct(name, Some(value)) + self.serialize_tuple_struct(name).and_then(|mut s| s.serialize_elt(Some(value))) } /// Allows a variant with a single item to be more efficiently @@ -187,8 +217,7 @@ pub trait Serializer { self.serialize_tuple_variant( name, variant_index, - variant, - Some(value)) + variant).and_then(|mut s| s.serialize_elt(Some(value))) } /// Serializes a `None` value..serialize @@ -202,21 +231,15 @@ pub trait Serializer { /// /// Callees of this method need to construct a `SeqVisitor`, which iterates through each item /// in the sequence. - fn serialize_seq(&mut self, visitor: V) -> Result<(), Self::Error> - where V: SeqVisitor; - - /// Serializes a sequence element. - fn serialize_seq_elt(&mut self, value: T) -> Result<(), Self::Error> - where T: Serialize; + fn serialize_seq(&mut self) -> Result; /// Serializes a tuple. /// /// By default this serializes a tuple as a sequence. #[inline] - fn serialize_tuple(&mut self, visitor: V) -> Result<(), Self::Error> - where V: SeqVisitor, + fn serialize_tuple(&mut self) -> Result { - self.serialize_seq(visitor) + self.serialize_seq() } /// Serializes a tuple element. @@ -226,29 +249,30 @@ pub trait Serializer { fn serialize_tuple_elt(&mut self, value: T) -> Result<(), Self::Error> where T: Serialize { - self.serialize_seq_elt(value) + self.serialize_seq().and_then(|mut s| s.serialize_elt(value)) } + /* + TODO: Fixed-sized visitor would kind of make sense here /// Serializes a fixed-size array. /// /// By default this serializes an array as a sequence. #[inline] - fn serialize_fixed_size_array(&mut self, visitor: V) -> Result<(), Self::Error> - where V: SeqVisitor, + fn serialize_fixed_size_array(&mut self, visitor: V) -> Result<(), Self::Error> { self.serialize_seq(visitor) } + */ /// Serializes a tuple struct. /// /// By default, tuple structs are serialized as a tuple. #[inline] - fn serialize_tuple_struct(&mut self, + fn serialize_tuple_struct(&mut self, _name: &'static str, - visitor: V) -> Result<(), Self::Error> - where V: SeqVisitor, + ) -> Result { - self.serialize_tuple(visitor) + self.serialize_tuple() } /// Serializes a tuple struct element. @@ -265,14 +289,13 @@ pub trait Serializer { /// /// By default, tuple variants are serialized as a tuple struct. #[inline] - fn serialize_tuple_variant(&mut self, + fn serialize_tuple_variant(&mut self, _name: &'static str, _variant_index: usize, variant: &'static str, - visitor: V) -> Result<(), Self::Error> - where V: SeqVisitor, + ) -> Result { - self.serialize_tuple_struct(variant, visitor) + self.serialize_tuple_struct(variant) } /// Serializes a tuple element. @@ -285,99 +308,30 @@ pub trait Serializer { self.serialize_tuple_struct_elt(value) } - /// Serializes a map. - /// - /// Callees of this method need to construct a `MapVisitor`, which iterates through each item - /// in the map. - fn serialize_map(&mut self, visitor: V) -> Result<(), Self::Error> - where V: MapVisitor; - - /// Serializes a map element (key-value pair). - fn serialize_map_elt(&mut self, key: K, value: V) -> Result<(), Self::Error> - where K: Serialize, - V: Serialize; + /// Serialize a map. + fn serialize_map(&mut self) -> Result; /// Serializes a struct. /// /// By default, structs are serialized as a map with the field name as the key. #[inline] - fn serialize_struct(&mut self, + fn serialize_struct(&mut self, _name: &'static str, - visitor: V) -> Result<(), Self::Error> - where V: MapVisitor, + ) -> Result { - self.serialize_map(visitor) - } - - /// Serializes an element of a struct. - /// - /// By default, struct elements are serialized as a map element with the field name as the key. - #[inline] - fn serialize_struct_elt(&mut self, - key: &'static str, - value: V) -> Result<(), Self::Error> - where V: Serialize, - { - self.serialize_map_elt(key, value) + self.serialize_map() } /// Serializes a struct variant. /// /// By default, struct variants are serialized as a struct. #[inline] - fn serialize_struct_variant(&mut self, + fn serialize_struct_variant(&mut self, _name: &'static str, _variant_index: usize, - variant: &'static str, - visitor: V) -> Result<(), Self::Error> - where V: MapVisitor, + variant: &'static str + ) -> Result { - self.serialize_struct(variant, visitor) - } - - /// Serializes an element of a struct variant. - /// - /// By default, struct variant elements are serialized as a struct element. - #[inline] - fn serialize_struct_variant_elt(&mut self, - key: &'static str, - value: V) -> Result<(), Self::Error> - where V: Serialize, - { - self.serialize_struct_elt(key, value) - } -} - -/// A trait that is used by a `Serialize` to iterate through a sequence. -#[cfg_attr(feature = "nightly-testing", allow(len_without_is_empty))] -pub trait SeqVisitor { - /// Serializes a sequence item in the serializer. - /// - /// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when - /// complete. - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer; - - /// Return the length of the sequence if known. - #[inline] - fn len(&self) -> Option { - None - } -} - -/// A trait that is used by a `Serialize` to iterate through a map. -#[cfg_attr(feature = "nightly-testing", allow(len_without_is_empty))] -pub trait MapVisitor { - /// Serializes a map item in the serializer. - /// - /// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when - /// complete. - fn visit(&mut self, serializer: &mut S) -> Result, S::Error> - where S: Serializer; - - /// Return the length of the map if known. - #[inline] - fn len(&self) -> Option { - None + self.serialize_struct(variant) } }