cbor
cbor copied to clipboard
Fix encoding of packed variants in new enum format
Fixes #187
Legacy enum format supported packed encoding pretty well but with the switch to new enum format there was a regression (#173). This commit fixes the new enum format in packed encoding.
For example consider the following structure:
enum Enum {
Unit,
NewType(i32),
Tuple(i32, i32),
Struct { x: i32, y: i32 },
}
Legacy enum packed encodings are:
Empty: <variant number>
NewType: [<variant number>, value]
Tuple: [<variant number>, values..]
Struct: [<variant number>, {<struct>}]
Non-legacy enum packed encodings before this commit:
Empty: <variant number>
NewType: {"<variant>": value}
Tuple: {"<variant>": [values..]}
Struct: {<variant number>: {<struct>}}
Notice how NewType and Tuple store the name instead of variant number.
Fixed after this commit:
Empty: <variant number>
NewType: {<variant number>: value}
Tuple: {<variant number>: [values..]}
Struct: {<variant number>: {<struct>}}
After this commit the packed encoding can be briefly described as: All struct fields and enum variants are encoded as their field number rather than name. This applies to all types of members (unit, newtype, tuple and struct).