Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to deserialize struct variants of untagged and adjacently tagged enums from seq #2465

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,30 +1008,28 @@ fn deserialize_struct(

// untagged struct variants do not get a visit_seq method. The same applies to
// structs that only have a map representation.
let visit_seq = match form {
StructForm::Untagged(..) => None,
_ if cattrs.has_flatten() => None,
_ => {
let mut_seq = if field_names_idents.is_empty() {
quote!(_)
} else {
quote!(mut __seq)
};
let visit_seq = if cattrs.has_flatten() {
None
} else {
let mut_seq = if field_names_idents.is_empty() {
quote!(_)
} else {
quote!(mut __seq)
};

let visit_seq = Stmts(deserialize_seq(
&type_path, params, fields, true, cattrs, expecting,
));
let visit_seq = Stmts(deserialize_seq(
&type_path, params, fields, true, cattrs, expecting,
));

Some(quote! {
#[inline]
fn visit_seq<__A>(self, #mut_seq: __A) -> _serde::__private::Result<Self::Value, __A::Error>
where
__A: _serde::de::SeqAccess<#delife>,
{
#visit_seq
}
})
}
Some(quote! {
#[inline]
fn visit_seq<__A>(self, #mut_seq: __A) -> _serde::__private::Result<Self::Value, __A::Error>
where
__A: _serde::de::SeqAccess<#delife>,
{
#visit_seq
}
})
};
let visit_map = Stmts(deserialize_map(&type_path, params, fields, cattrs));

Expand Down
9 changes: 4 additions & 5 deletions test_suite/tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ fn test_untagged_enum() {
Token::StructEnd,
],
);
assert_de_tokens(
&Untagged::A { a: 1 },
&[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
);

assert_tokens(
&Untagged::B { b: 2 },
Expand Down Expand Up @@ -660,11 +664,6 @@ fn test_untagged_enum() {
],
);

assert_de_tokens_error::<Untagged>(
&[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
"data did not match any variant of untagged enum Untagged",
);

assert_de_tokens_error::<Untagged>(
&[
Token::Tuple { len: 3 },
Expand Down