Skip to content

Commit

Permalink
Allow to deserialize unit structs from the empty sequences and maps
Browse files Browse the repository at this point in the history
Otherwise the following tests will fail:
- test_internally_tagged_newtype_variant_containing_unit_struct
- test_internally_tagged_struct_variant_containing_unit_variant

This reverts commit 1986c17.

failures (2):
    newtype_variant_containing_unit
    struct_variant_containing_unit_variant
  • Loading branch information
Mingun committed Oct 21, 2024
1 parent 89e2b36 commit 132dc81
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
37 changes: 37 additions & 0 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,43 @@ fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fra
{
_serde::__private::Ok(#this_value)
}

#[inline]
fn visit_seq<__A>(self, mut __seq: __A) -> _serde::__private::Result<Self::Value, __A::Error>
where
__A: _serde::de::SeqAccess<'de>,
{
// Allow to deserialize unit from empty sequence
if _serde::de::SeqAccess::next_element::<
_serde::de::IgnoredAny,
>(&mut __seq)?.is_some() {
_serde::__private::Err(_serde::de::Error::invalid_type(
_serde::de::Unexpected::Seq,
&self
))
} else {
_serde::__private::Ok(#this_value)
}
}

#[inline]
fn visit_map<__A>(self, mut __map: __A) -> _serde::__private::Result<Self::Value, __A::Error>
where
__A: _serde::de::MapAccess<'de>,
{
// Allow to deserialize unit from empty map
if _serde::de::MapAccess::next_entry::<
_serde::de::IgnoredAny,
_serde::de::IgnoredAny,
>(&mut __map)?.is_some() {
_serde::__private::Err(_serde::de::Error::invalid_type(
_serde::de::Unexpected::Map,
&self
))
} else {
_serde::__private::Ok(#this_value)
}
}
}

_serde::Deserializer::deserialize_unit_struct(
Expand Down
14 changes: 14 additions & 0 deletions test_suite/tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,20 @@ fn test_unit() {
fn test_unit_struct() {
test(UnitStruct, &[Token::Unit]);
test(UnitStruct, &[Token::UnitStruct { name: "UnitStruct" }]);
test(UnitStruct, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
test(UnitStruct, &[Token::Seq { len: None }, Token::SeqEnd]);
test(UnitStruct, &[Token::Map { len: Some(0) }, Token::MapEnd]);
test(UnitStruct, &[Token::Map { len: None }, Token::MapEnd]);
test(
UnitStruct,
&[
Token::Struct {
name: "ZeroStruct",
len: 0,
},
Token::StructEnd,
],
);
}

#[test]
Expand Down
8 changes: 0 additions & 8 deletions test_suite/tests/test_de_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,14 +1446,6 @@ fn test_nan_no_decimal_point() {
);
}

#[test]
fn test_unit_struct_from_seq() {
assert_de_tokens_error::<UnitStruct>(
&[Token::Seq { len: Some(0) }, Token::SeqEnd],
"invalid type: sequence, expected unit struct UnitStruct",
);
}

#[test]
fn test_wrapping_overflow() {
assert_de_tokens_error::<Wrapping<u16>>(
Expand Down

0 comments on commit 132dc81

Please sign in to comment.