Skip to content

Unify handling of unit and unit structs in ContentDeserializer - #3110

Open
Mingun wants to merge 2 commits into
serde-rs:masterfrom
Mingun:unify-handling
Open

Mingun wants to merge 2 commits into
serde-rs:masterfrom
Mingun:unify-handling

Conversation

@Mingun

@Mingun Mingun commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

That changed path could be reached by deserialization of value:

  • of newtype variant of internally tagged enum
  • of content of adjacently tagged enum. So,
    { "content": [],   "tag": ... }
    will be deserialized the same, as
    { "content": {},   "tag": ... }
    or
    { "content": null, "tag": ... }
  • of field in the flatten struct

See the playground which demonstrates the current behavior. Note, that tests with unit structs are all passed, but some tests with () are not:

  • adjacently::unit_seq
  • internally::unit_seq
  • flatten::unit_seq

This PR fixes this behavior. Note, that probably internally tagged enums, content of the adjacently tagged enums and content of flatten types must use different deserializers, but that change is out of scope of this PR.

…ly tagged enum from sequence of 1 element (tag only)

After consuming the tag from the sequence the value (`()`) should be deserialzied from an empty sequence

failures(1):
    newtype_unit
That path could be reached by deserialization of value:
- of newtype variant of internally tagged enum
- of content of adjacently tagged enum. So,
  { "content": [],   "tag": ... } will be deserialized the same, as
  { "content": {},   "tag": ... } or
  { "content": null, "tag": ... }
- of field in the flatten struct
Comment on lines +160 to +176

assert_de_tokens(
&value,
&[
Token::Seq { len: Some(1) },
Token::Str("NewtypeUnit"), // tag
Token::SeqEnd,
],
);
assert_de_tokens(
&value,
&[
Token::Seq { len: Some(1) },
Token::BorrowedStr("NewtypeUnit"), // tag
Token::SeqEnd,
],
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same tests for unit struct currently at lines

assert_de_tokens(
&value,
&[
Token::Seq { len: Some(1) },
Token::Str("NewtypeUnitStruct"), // tag
Token::SeqEnd,
],
);
assert_de_tokens(
&value,
&[
Token::Seq { len: Some(1) },
Token::BorrowedStr("NewtypeUnitStruct"), // tag
Token::SeqEnd,
],
);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant