cbor icon indicating copy to clipboard operation
cbor copied to clipboard

Fix encoding of packed variants in new enum format

Open matix2267 opened this issue 3 years ago • 0 comments

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).

matix2267 avatar Jun 14 '21 12:06 matix2267