Merge pull request #684 from serde-rs/end

Remove SeqVisitor::end() and MapVisitor::end()
This commit is contained in:
Oliver Schneider 2017-01-14 08:01:53 +01:00 committed by GitHub
commit 787ecc6a82
7 changed files with 101 additions and 174 deletions

View File

@ -19,8 +19,8 @@ script:
- (cd testing && travis-cargo --skip nightly test)
- (cd testing && travis-cargo --only nightly test -- --features unstable-testing)
- (cd serde_derive && travis-cargo --only nightly test)
- (cd examples/serde-syntex-example && travis-cargo --skip nightly run)
- (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --no-default-features --features unstable)
#- (cd examples/serde-syntex-example && travis-cargo --skip nightly run)
#- (cd examples/serde-syntex-example && travis-cargo --only nightly run -- --no-default-features --features unstable)
- (cd serde && travis-cargo --only stable doc)
env:
global:

View File

@ -199,8 +199,6 @@ mod bytebuf {
values.push(value);
}
try!(visitor.end());
Ok(ByteBuf::from(values))
}

View File

@ -88,10 +88,10 @@ impl Visitor for UnitVisitor {
Ok(())
}
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), V::Error>
fn visit_seq<V>(&mut self, _: V) -> Result<(), V::Error>
where V: SeqVisitor,
{
visitor.end()
Ok(())
}
}
@ -433,8 +433,6 @@ macro_rules! seq_impl {
$insert(&mut values, value);
}
try!($visitor.end());
Ok(values)
}
}
@ -540,10 +538,9 @@ impl<T> Visitor for ArrayVisitor<[T; 0]> where T: Deserialize {
}
#[inline]
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<[T; 0], V::Error>
fn visit_seq<V>(&mut self, _: V) -> Result<[T; 0], V::Error>
where V: SeqVisitor,
{
try!(visitor.end());
Ok([])
}
}
@ -575,8 +572,6 @@ macro_rules! array_impls {
};
)+
try!(visitor.end());
Ok([$($name),+])
}
}
@ -661,8 +656,6 @@ macro_rules! tuple_impls {
};
)+
try!(visitor.end());
Ok(($($name,)+))
}
}
@ -746,8 +739,6 @@ macro_rules! map_impl {
values.insert(key, value);
}
try!($visitor.end());
Ok(values)
}
}
@ -1034,18 +1025,15 @@ impl Deserialize for Duration {
let secs: u64 = match try!(visitor.visit()) {
Some(value) => value,
None => {
try!(visitor.end());
return Err(Error::invalid_length(0));
}
};
let nanos: u32 = match try!(visitor.visit()) {
Some(value) => value,
None => {
try!(visitor.end());
return Err(Error::invalid_length(1));
}
};
try!(visitor.end());
Ok(Duration::new(secs, nanos))
}
@ -1070,7 +1058,6 @@ impl Deserialize for Duration {
}
}
}
try!(visitor.end());
let secs = match secs {
Some(secs) => secs,
None => try!(visitor.missing_field("secs")),
@ -1274,8 +1261,6 @@ impl Deserialize for IgnoredAny {
while let Some(_) = try!(visitor.visit::<IgnoredAny>()) {
// Gobble
}
try!(visitor.end());
Ok(IgnoredAny)
}
@ -1286,8 +1271,6 @@ impl Deserialize for IgnoredAny {
while let Some((_, _)) = try!(visitor.visit::<IgnoredAny, IgnoredAny>()) {
// Gobble
}
try!(visitor.end());
Ok(IgnoredAny)
}

View File

@ -635,9 +635,6 @@ pub trait SeqVisitor {
fn visit<T>(&mut self) -> Result<Option<T>, Self::Error>
where T: Deserialize;
/// This signals to the `SeqVisitor` that the `Visitor` does not expect any more items.
fn end(&mut self) -> Result<(), Self::Error>;
/// Return the lower and upper bound of items remaining in the sequence.
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
@ -655,11 +652,6 @@ impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
(**self).visit()
}
#[inline]
fn end(&mut self) -> Result<(), V::Error> {
(**self).end()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()
@ -700,9 +692,6 @@ pub trait MapVisitor {
fn visit_value<V>(&mut self) -> Result<V, Self::Error>
where V: Deserialize;
/// This signals to the `MapVisitor` that the `Visitor` does not expect any more items.
fn end(&mut self) -> Result<(), Self::Error>;
/// Return the lower and upper bound of items remaining in the sequence.
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
@ -742,11 +731,6 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
(**self).visit_value()
}
#[inline]
fn end(&mut self) -> Result<(), V_::Error> {
(**self).end()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(**self).size_hint()

View File

@ -41,7 +41,7 @@ use error;
use core::fmt;
use core::marker::PhantomData;
use de;
use de::{self, SeqVisitor};
use bytes;
///////////////////////////////////////////////////////////////////////////////
@ -543,7 +543,12 @@ impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
visitor.visit_seq(self)
let v = try!(visitor.visit_seq(&mut *self));
if self.len == 0 {
Ok(v)
} else {
Err(de::Error::invalid_length(self.len))
}
}
forward_to_deserialize! {
@ -573,14 +578,6 @@ impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
}
}
fn end(&mut self) -> Result<(), Self::Error> {
if self.len == 0 {
Ok(())
} else {
Err(de::Error::invalid_length(self.len))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
@ -715,6 +712,13 @@ impl<I, K, V, E> MapDeserializer<I, K, V, E>
(k, v)
})
}
fn end(&mut self) -> Result<(), E> {
match self.len {
Some(len) if len > 0 => Err(de::Error::invalid_length(len)),
_ => Ok(())
}
}
}
impl<I, K, V, E> de::Deserializer for MapDeserializer<I, K, V, E>
@ -728,22 +732,29 @@ impl<I, K, V, E> de::Deserializer for MapDeserializer<I, K, V, E>
fn deserialize<V_>(&mut self, mut visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
{
visitor.visit_map(self)
let value = try!(visitor.visit_map(&mut *self));
try!(self.end());
Ok(value)
}
fn deserialize_seq<V_>(&mut self, mut visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
{
visitor.visit_seq(self)
let value = try!(visitor.visit_seq(&mut *self));
try!(self.end());
Ok(value)
}
fn deserialize_seq_fixed_size<V_>(&mut self, len: usize, mut visitor: V_) -> Result<V_::Value, Self::Error>
where V_: de::Visitor,
{
match self.len {
Some(map_len) if map_len == len => visitor.visit_seq(self),
Some(_) => Err(de::Error::invalid_length(len)),
None => visitor.visit_seq(self),
Some(map_len) if map_len != len => Err(de::Error::invalid_length(len)),
_ => {
let value = try!(visitor.visit_seq(&mut *self));
try!(self.end());
Ok(value)
}
}
}
@ -789,13 +800,6 @@ impl<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
}
}
fn end(&mut self) -> Result<(), Self::Error> {
match self.len {
Some(len) if len > 0 => Err(de::Error::invalid_length(len)),
_ => Ok(())
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.len.map_or_else(
|| self.iter.size_hint(),
@ -823,10 +827,6 @@ impl<I, K, V, E> de::SeqVisitor for MapDeserializer<I, K, V, E>
}
}
fn end(&mut self) -> Result<(), Self::Error> {
de::MapVisitor::end(self)
}
fn size_hint(&self) -> (usize, Option<usize>) {
de::MapVisitor::size_hint(self)
}
@ -860,7 +860,13 @@ impl<A, B, E> de::Deserializer for PairDeserializer<A, B, E>
{
match self.0.take() {
Some((k, v)) => {
visitor.visit_seq(PairVisitor(Some(k), Some(v), PhantomData))
let mut pair_visitor = PairVisitor(Some(k), Some(v), PhantomData);
let pair = try!(visitor.visit_seq(&mut pair_visitor));
if pair_visitor.1.is_none() {
Ok(pair)
} else {
Err(de::Error::invalid_length(pair_visitor.size_hint().0))
}
}
None => Err(de::Error::end_of_stream()),
}
@ -900,14 +906,6 @@ impl<A, B, E> de::SeqVisitor for PairVisitor<A, B, E>
}
}
fn end(&mut self) -> Result<(), Self::Error> {
if self.1.is_none() {
Ok(())
} else {
Err(de::Error::invalid_length(self.size_hint().0))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = if self.0.is_some() {
2

View File

@ -214,11 +214,10 @@ fn deserialize_unit_struct(
}
#[inline]
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#type_ident, __V::Error>
fn visit_seq<__V>(&mut self, _: __V) -> ::std::result::Result<#type_ident, __V::Error>
where __V: _serde::de::SeqVisitor,
{
try!(visitor.end());
self.visit_unit()
Ok(#type_ident)
}
}
@ -275,6 +274,13 @@ fn deserialize_tuple(
quote!(deserializer.deserialize_tuple_struct(#type_name, #nfields, #visitor_expr))
};
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
let visitor_var = if all_skipped {
quote!(_)
} else {
quote!(mut visitor)
};
quote!({
#visitor_item
@ -284,7 +290,7 @@ fn deserialize_tuple(
#visit_newtype_struct
#[inline]
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error>
fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error>
where __V: _serde::de::SeqVisitor
{
#visit_seq
@ -332,7 +338,6 @@ fn deserialize_seq(
let #var = match #visit {
Some(value) => { value },
None => {
try!(visitor.end());
return Err(_serde::de::Error::invalid_length(#index_in_seq));
}
};
@ -355,9 +360,6 @@ fn deserialize_seq(
quote! {
#(#let_values)*
try!(visitor.end());
Ok(#result)
}
}
@ -440,6 +442,13 @@ fn deserialize_struct(
}
};
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
let visitor_var = if all_skipped {
quote!(_)
} else {
quote!(mut visitor)
};
quote!({
#field_visitor
@ -449,7 +458,7 @@ fn deserialize_struct(
type Value = #ty;
#[inline]
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error>
fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error>
where __V: _serde::de::SeqVisitor
{
#visit_seq
@ -842,8 +851,6 @@ fn deserialize_map(
#match_keys
try!(visitor.end());
#(#extract_values)*
Ok(#struct_path { #(#result),* })

View File

@ -31,76 +31,105 @@ impl<I> Deserializer<I>
self.tokens.next()
}
pub fn expect_token(&mut self, expected: Token) -> Result<(), Error> {
match self.tokens.next() {
Some(token) => {
if expected == token {
Ok(())
} else {
Err(Error::UnexpectedToken(token))
}
}
None => Err(Error::EndOfStream),
}
}
fn visit_seq<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_seq(DeserializerSeqVisitor {
let value = try!(visitor.visit_seq(DeserializerSeqVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::SeqEnd));
Ok(value)
}
fn visit_array<V>(&mut self, len: usize, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_seq(DeserializerArrayVisitor {
let value = try!(visitor.visit_seq(DeserializerArrayVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::SeqEnd));
Ok(value)
}
fn visit_tuple<V>(&mut self, len: usize, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_seq(DeserializerTupleVisitor {
let value = try!(visitor.visit_seq(DeserializerTupleVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::TupleEnd));
Ok(value)
}
fn visit_tuple_struct<V>(&mut self, len: usize, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_seq(DeserializerTupleStructVisitor {
let value = try!(visitor.visit_seq(DeserializerTupleStructVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::TupleStructEnd));
Ok(value)
}
fn visit_variant_seq<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_seq(DeserializerVariantSeqVisitor {
let value = try!(visitor.visit_seq(DeserializerVariantSeqVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::EnumSeqEnd));
Ok(value)
}
fn visit_map<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_map(DeserializerMapVisitor {
let value = try!(visitor.visit_map(DeserializerMapVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::MapEnd));
Ok(value)
}
fn visit_struct<V>(&mut self, fields: &'static [&'static str], mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_map(DeserializerStructVisitor {
let value = try!(visitor.visit_map(DeserializerStructVisitor {
de: self,
len: fields.len(),
})
}));
try!(self.expect_token(Token::StructEnd));
Ok(value)
}
fn visit_variant_map<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_map(DeserializerVariantMapVisitor {
let value = try!(visitor.visit_map(DeserializerVariantMapVisitor {
de: self,
len: len,
})
}));
try!(self.expect_token(Token::EnumMapEnd));
Ok(value)
}
}
@ -478,15 +507,6 @@ impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I>
}
}
fn end(&mut self) -> Result<(), Error> {
//assert_eq!(self.len.unwrap_or(0), 0);
match self.de.tokens.next() {
Some(Token::SeqEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len.unwrap_or(0);
(len, self.len)
@ -523,15 +543,6 @@ impl<'a, I> SeqVisitor for DeserializerArrayVisitor<'a, I>
}
}
fn end(&mut self) -> Result<(), Error> {
assert_eq!(self.len, 0);
match self.de.tokens.next() {
Some(Token::SeqEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
@ -567,15 +578,6 @@ impl<'a, I> SeqVisitor for DeserializerTupleVisitor<'a, I>
}
}
fn end(&mut self) -> Result<(), Error> {
assert_eq!(self.len, 0);
match self.de.tokens.next() {
Some(Token::TupleEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
@ -611,15 +613,6 @@ impl<'a, I> SeqVisitor for DeserializerTupleStructVisitor<'a, I>
}
}
fn end(&mut self) -> Result<(), Error> {
assert_eq!(self.len, 0);
match self.de.tokens.next() {
Some(Token::TupleStructEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
@ -655,15 +648,6 @@ impl<'a, I> SeqVisitor for DeserializerVariantSeqVisitor<'a, I>
}
}
fn end(&mut self) -> Result<(), Error> {
//assert_eq!(self.len.unwrap_or(0), 0);
match self.de.tokens.next() {
Some(Token::EnumSeqEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len.unwrap_or(0);
(len, self.len)
@ -706,15 +690,6 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
Ok(try!(Deserialize::deserialize(self.de)))
}
fn end(&mut self) -> Result<(), Error> {
//assert_eq!(self.len.unwrap_or(0), 0);
match self.de.tokens.next() {
Some(Token::MapEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len.unwrap_or(0);
(len, self.len)
@ -757,15 +732,6 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I>
Ok(try!(Deserialize::deserialize(self.de)))
}
fn end(&mut self) -> Result<(), Error> {
//assert_eq!(self.len.unwrap_or(0), 0);
match self.de.tokens.next() {
Some(Token::StructEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
@ -928,15 +894,6 @@ impl<'a, I> MapVisitor for DeserializerVariantMapVisitor<'a, I>
Ok(try!(Deserialize::deserialize(self.de)))
}
fn end(&mut self) -> Result<(), Error> {
//assert_eq!(self.len.unwrap_or(0), 0);
match self.de.tokens.next() {
Some(Token::EnumMapEnd) => Ok(()),
Some(token) => Err(Error::UnexpectedToken(token)),
None => Err(Error::EndOfStream),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len.unwrap_or(0);
(len, self.len)