From c3d9b42cdf402bf249a1b3f272451694cdf21182 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 5 Apr 2017 09:19:22 -0700 Subject: [PATCH] Fix handling of option and newtype in IgnoredAny --- serde/src/de/ignored_any.rs | 8 ++++---- serde_test/src/de.rs | 3 ++- test_suite/tests/test_de.rs | 17 +++++++++-------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/serde/src/de/ignored_any.rs b/serde/src/de/ignored_any.rs index d51f7386..3ee68433 100644 --- a/serde/src/de/ignored_any.rs +++ b/serde/src/de/ignored_any.rs @@ -53,17 +53,17 @@ impl<'de> Deserialize<'de> for IgnoredAny { } #[inline] - fn visit_some(self, _: D) -> Result + fn visit_some(self, deserializer: D) -> Result where D: Deserializer<'de> { - Ok(IgnoredAny) + IgnoredAny::deserialize(deserializer) } #[inline] - fn visit_newtype_struct(self, _: D) -> Result + fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de> { - Ok(IgnoredAny) + IgnoredAny::deserialize(deserializer) } #[inline] diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index 5437ffa2..28989187 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -111,6 +111,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { Some(Token::Option(true)) => visitor.visit_some(self), Some(Token::Unit) => visitor.visit_unit(), Some(Token::UnitStruct(_name)) => visitor.visit_unit(), + Some(Token::StructNewType(_name)) => visitor.visit_newtype_struct(self), Some(Token::SeqStart(len)) => { self.visit_seq(len, Token::SeqSep, Token::SeqEnd, visitor) } @@ -182,7 +183,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { self.next_token(); visitor.visit_some(self) } - Some(_) => visitor.visit_some(self), + Some(_) => self.deserialize(visitor), None => Err(Error::EndOfTokens), } } diff --git a/test_suite/tests/test_de.rs b/test_suite/tests/test_de.rs index f5817f2b..2d012259 100644 --- a/test_suite/tests/test_de.rs +++ b/test_suite/tests/test_de.rs @@ -35,6 +35,9 @@ mod macros; #[derive(Copy, Clone, PartialEq, Debug, Deserialize)] struct UnitStruct; +#[derive(PartialEq, Debug, Deserialize)] +struct NewtypeStruct(i32); + #[derive(PartialEq, Debug, Deserialize)] struct TupleStruct(i32, i32, i32); @@ -166,13 +169,6 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) { let mut de = serde_test::Deserializer::new(&concated_tokens); let v: Result = Deserialize::deserialize(&mut de); - // We run this test on every token stream for convenience, but - // some token streams don't make sense embedded as a map value, - // so we ignore those. SyntaxError is the real sign of trouble. - if let Err(Error::UnexpectedToken(_)) = v { - return; - } - assert_eq!(v.as_ref(), Ok(&expected)); assert_eq!(de.next_token(), None); } @@ -225,7 +221,6 @@ declare_tests! { test_option { None:: => &[Token::Unit], None:: => &[Token::Option(false)], - Some(1) => &[Token::I32(1)], Some(1) => &[ Token::Option(true), Token::I32(1), @@ -260,6 +255,12 @@ declare_tests! { Token::SeqEnd, ], } + test_newtype_struct { + NewtypeStruct(1) => &[ + Token::StructNewType("NewtypeStruct"), + Token::I32(1), + ], + } test_tuple_struct { TupleStruct(1, 2, 3) => &[ Token::SeqStart(Some(3)),