From 171c6da57af712cfcf01c6c124b14cabfca364ba Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 17 Aug 2024 11:31:50 +0500 Subject: [PATCH] Complete coverage of ContentRefDeserializer::deserialize_newtype_struct --- serde/src/private/de.rs | 7 +++++++ test_suite/tests/test_enum_untagged.rs | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 5b5d9b6a1..846647e0c 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1938,10 +1938,17 @@ mod content { where V: Visitor<'de>, { + // Covered by tests/test_enum_untagged.rs + // newtype_struct match *self.content { Content::Newtype(ref v) => { visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) } + // This case is necessary for formats which does not store marker of a newtype, + // for example, JSON. When `deserialize_any` is requested from such formats, they will + // report value without using `Visitor::visit_newtype_struct`, because they do not + // known in which contexts this value will be used. + // RON is example of format which preserve markers. _ => visitor.visit_newtype_struct(self), } } diff --git a/test_suite/tests/test_enum_untagged.rs b/test_suite/tests/test_enum_untagged.rs index 60c62bac6..9079eeb60 100644 --- a/test_suite/tests/test_enum_untagged.rs +++ b/test_suite/tests/test_enum_untagged.rs @@ -104,6 +104,7 @@ fn newtype_unit_and_empty_map() { ); } +// Reaches crate::private::de::content::ContentRefDeserializer::deserialize_newtype_struct #[test] fn newtype_struct() { #[derive(Debug, PartialEq, Serialize, Deserialize)] @@ -116,8 +117,11 @@ fn newtype_struct() { Null, } + let value = E::Newtype(NewtypeStruct(5)); + + // Content::Newtype case assert_tokens( - &E::Newtype(NewtypeStruct(5)), + &value, &[ Token::NewtypeStruct { name: "NewtypeStruct", @@ -125,6 +129,9 @@ fn newtype_struct() { Token::U32(5), ], ); + + // _ case + assert_de_tokens(&value, &[Token::U32(5)]); } #[test]