serde_bser: serialize struct enum variants in object form - #1358
Open
rootkiller6788 wants to merge 1 commit into
Open
serde_bser: serialize struct enum variants in object form#1358rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
serialize_struct_variant delegated to serialize_tuple_variant, emitting a
struct variant as {"Variant": [field_name, value, ...]} instead of the
{"Variant": {"field": value, ...}} form that the deserializer expects
when decoding a struct variant. Enums with struct variants therefore could
not round-trip through serde_bser (deserializing the emitted bytes failed
with a type mismatch).
Emit the variant as an object keyed by field name, matching the format
already exercised by de::test::test_struct_variant, and add a round-trip
regression test.
|
This pull request has been imported. If you are a Meta employee, you can view this in D116917626. (Because this pull request was imported automatically, there will not be any future comments.) |
rootkiller6788
marked this pull request as ready for review
August 22, 2026 14:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
serde_bser::Serializer::serialize_struct_variantwas implemented by delegating toserialize_tuple_variant, which emits the variant payload as a BSER array:{"TestStruct": ["abc", null, "def", "💩"]}But the deserializer (
VariantAccess::struct_variant→deserialize_any) decodes the payload as a map — the object form{"TestStruct": {"abc": null, "def": "💩"}}thatde::test::test_struct_variantalready exercises. Struct-variant enums therefore could not round-trip:serialize→from_slicefailed withinvalid type: string "abc", expected unit.Fix
Serialize struct variants in object form, writing
<BSER_OBJECT> 1 <variant name> <BSER_OBJECT nfields> (key value)*, instead of reusing the tuple-variant path. TheCompoundtype is shared by bothSerializeStructVariantandSerializeStruct, so the field-name-keyed output is produced by the sameSerializeStructmachinery used for ordinary structs.Test
BASIC_SERIALIZEDexpectation inser/test.rsto the object form (only the container tag byte changes; PDU length is unchanged).test_struct_variant_round_trip, which serializesTestStruct { abc: (), def: '💩' }and deserializes it back — this failed before the fix and passes after.cargo testinwatchman/rust/serde_bser: 16 passed.