Skip to content

Commit

Permalink
Complete coverage of ContentRefDeserializer::deserialize_newtype_struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Aug 23, 2024
1 parent 2dddc77 commit 171c6da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
9 changes: 8 additions & 1 deletion test_suite/tests/test_enum_untagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -116,15 +117,21 @@ fn newtype_struct() {
Null,
}

let value = E::Newtype(NewtypeStruct(5));

// Content::Newtype case
assert_tokens(
&E::Newtype(NewtypeStruct(5)),
&value,
&[
Token::NewtypeStruct {
name: "NewtypeStruct",
},
Token::U32(5),
],
);

// _ case
assert_de_tokens(&value, &[Token::U32(5)]);
}

#[test]
Expand Down

0 comments on commit 171c6da

Please sign in to comment.