Remove the first argument from visit_{seq,map}_elt

This commit is contained in:
Erick Tryzelaar
2015-03-16 22:30:02 -07:00
parent a6ba251ef9
commit b40d8f7bac
6 changed files with 271 additions and 79 deletions
+16 -4
View File
@@ -9,6 +9,10 @@ use ser;
pub struct Serializer<W, F=CompactFormatter> {
writer: W,
formatter: F,
/// `first` is used to signify if we should print a comma when we are walking through a
/// sequence.
first: bool,
}
impl<W> Serializer<W>
@@ -33,6 +37,7 @@ impl<W, F> Serializer<W, F>
Serializer {
writer: writer,
formatter: formatter,
first: false,
}
}
@@ -166,6 +171,8 @@ impl<W, F> ser::Serializer for Serializer<W, F>
_ => {
try!(self.formatter.open(&mut self.writer, b'['));
self.first = true;
while let Some(()) = try!(visitor.visit(self)) { }
self.formatter.close(&mut self.writer, b']')
@@ -187,10 +194,11 @@ impl<W, F> ser::Serializer for Serializer<W, F>
}
#[inline]
fn visit_seq_elt<T>(&mut self, first: bool, value: T) -> io::Result<()>
fn visit_seq_elt<T>(&mut self, value: T) -> io::Result<()>
where T: ser::Serialize,
{
try!(self.formatter.comma(&mut self.writer, first));
try!(self.formatter.comma(&mut self.writer, self.first));
self.first = false;
value.serialize(self)
}
@@ -206,6 +214,8 @@ impl<W, F> ser::Serializer for Serializer<W, F>
_ => {
try!(self.formatter.open(&mut self.writer, b'{'));
self.first = true;
while let Some(()) = try!(visitor.visit(self)) { }
self.formatter.close(&mut self.writer, b'}')
@@ -227,11 +237,13 @@ impl<W, F> ser::Serializer for Serializer<W, F>
}
#[inline]
fn visit_map_elt<K, V>(&mut self, first: bool, key: K, value: V) -> io::Result<()>
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> io::Result<()>
where K: ser::Serialize,
V: ser::Serialize,
{
try!(self.formatter.comma(&mut self.writer, first));
try!(self.formatter.comma(&mut self.writer, self.first));
self.first = false;
try!(key.serialize(self));
try!(self.formatter.colon(&mut self.writer));
value.serialize(self)
+2 -2
View File
@@ -271,7 +271,7 @@ impl ser::Serializer for Serializer {
}
#[inline]
fn visit_seq_elt<T>(&mut self, _first: bool, value: T) -> Result<(), ()>
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), ()>
where T: ser::Serialize,
{
try!(value.serialize(self));
@@ -330,7 +330,7 @@ impl ser::Serializer for Serializer {
}
#[inline]
fn visit_map_elt<K, V>(&mut self, _first: bool, key: K, value: V) -> Result<(), ()>
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), ()>
where K: ser::Serialize,
V: ser::Serialize,
{
+5 -25
View File
@@ -124,9 +124,7 @@ pub trait Serializer {
self.visit_seq(visitor)
}
fn visit_seq_elt<T>(&mut self,
first: bool,
value: T) -> Result<(), Self::Error>
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
where T: Serialize;
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
@@ -151,10 +149,7 @@ pub trait Serializer {
self.visit_map(visitor)
}
fn visit_map_elt<K, V>(&mut self,
first: bool,
key: K,
value: V) -> Result<(), Self::Error>
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where K: Serialize,
V: Serialize;
}
@@ -250,7 +245,6 @@ impl<T> Serialize for Option<T> where T: Serialize {
pub struct SeqIteratorVisitor<Iter> {
iter: Iter,
len: Option<usize>,
first: bool,
}
impl<T, Iter> SeqIteratorVisitor<Iter>
@@ -261,7 +255,6 @@ impl<T, Iter> SeqIteratorVisitor<Iter>
SeqIteratorVisitor {
iter: iter,
len: len,
first: true,
}
}
}
@@ -274,12 +267,9 @@ impl<T, Iter> SeqVisitor for SeqIteratorVisitor<Iter>
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: Serializer,
{
let first = self.first;
self.first = false;
match self.iter.next() {
Some(value) => {
let value = try!(serializer.visit_seq_elt(first, value));
let value = try!(serializer.visit_seq_elt(value));
Ok(Some(value))
}
None => Ok(None),
@@ -365,7 +355,6 @@ macro_rules! tuple_impls {
pub struct $TupleVisitor<'a, $($T: 'a),+> {
tuple: &'a ($($T,)+),
state: u8,
first: bool,
}
impl<'a, $($T: 'a),+> $TupleVisitor<'a, $($T),+> {
@@ -373,7 +362,6 @@ macro_rules! tuple_impls {
$TupleVisitor {
tuple: tuple,
state: 0,
first: true,
}
}
}
@@ -384,14 +372,11 @@ macro_rules! tuple_impls {
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: Serializer,
{
let first = self.first;
self.first = false;
match self.state {
$(
$state => {
self.state += 1;
Ok(Some(try!(serializer.visit_seq_elt(first, &e!(self.tuple.$idx)))))
Ok(Some(try!(serializer.visit_seq_elt(&e!(self.tuple.$idx)))))
}
)+
_ => {
@@ -527,7 +512,6 @@ tuple_impls! {
pub struct MapIteratorVisitor<Iter> {
iter: Iter,
len: Option<usize>,
first: bool,
}
impl<K, V, Iter> MapIteratorVisitor<Iter>
@@ -538,7 +522,6 @@ impl<K, V, Iter> MapIteratorVisitor<Iter>
MapIteratorVisitor {
iter: iter,
len: len,
first: true,
}
}
}
@@ -552,12 +535,9 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: Serializer,
{
let first = self.first;
self.first = false;
match self.iter.next() {
Some((key, value)) => {
let value = try!(serializer.visit_map_elt(first, key, value));
let value = try!(serializer.visit_map_elt(key, value));
Ok(Some(value))
}
None => Ok(None)