Skip to content

Commit 171c6da

Browse files
committed
Complete coverage of ContentRefDeserializer::deserialize_newtype_struct
1 parent 2dddc77 commit 171c6da

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

serde/src/private/de.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,10 +1938,17 @@ mod content {
19381938
where
19391939
V: Visitor<'de>,
19401940
{
1941+
// Covered by tests/test_enum_untagged.rs
1942+
// newtype_struct
19411943
match *self.content {
19421944
Content::Newtype(ref v) => {
19431945
visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
19441946
}
1947+
// This case is necessary for formats which does not store marker of a newtype,
1948+
// for example, JSON. When `deserialize_any` is requested from such formats, they will
1949+
// report value without using `Visitor::visit_newtype_struct`, because they do not
1950+
// known in which contexts this value will be used.
1951+
// RON is example of format which preserve markers.
19451952
_ => visitor.visit_newtype_struct(self),
19461953
}
19471954
}

test_suite/tests/test_enum_untagged.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ fn newtype_unit_and_empty_map() {
104104
);
105105
}
106106

107+
// Reaches crate::private::de::content::ContentRefDeserializer::deserialize_newtype_struct
107108
#[test]
108109
fn newtype_struct() {
109110
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -116,15 +117,21 @@ fn newtype_struct() {
116117
Null,
117118
}
118119

120+
let value = E::Newtype(NewtypeStruct(5));
121+
122+
// Content::Newtype case
119123
assert_tokens(
120-
&E::Newtype(NewtypeStruct(5)),
124+
&value,
121125
&[
122126
Token::NewtypeStruct {
123127
name: "NewtypeStruct",
124128
},
125129
Token::U32(5),
126130
],
127131
);
132+
133+
// _ case
134+
assert_de_tokens(&value, &[Token::U32(5)]);
128135
}
129136

130137
#[test]

0 commit comments

Comments
 (0)