- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Description
This issue is closely related to #17158, but affects the proposed RustcEncodable trait which will be stabilized as part of the compiler for Rust 1.0.
Currently, the code which derives Encodable has a few weird design decisions:
1. Rust always uses emit_enum_variant instead of emit_enum_struct_variant.
If we have a structure ExEnum as follows:
#[deriving(Encodable)]
enum ExEnum { Foo, Bar(f64), Baz{x: f64, y: f64} }…then both Bar and Baz will emit code to call emit_enum_variant. For example, JSON encoding will yield:
"Foo"
{"variant":"Bar","fields":[1]}
{"variant":"Baz","fields":[1,2]}
This would probably look a lot nicer with x: and y: in the Baz case.
2. Rust will always use emit_struct instead of emit_tuple_struct
#[deriving(Encodable)]
struct ExTupleStruct(f64);This will encode to JSON as:
{"_field0":1}
This is particularly unfortunate—the resulting data would look nicer if it went through emit_tuple_struct and used some sort of array-based encoding.
Proposed fixes
I can see two ways forward:
- Option 1: Officially drop emit_enum_struct_variantandemit_tuple_structand always useemit_enum_variantandemit_struct.
- Option 2: Fix the Encoder(or the newRustcEncoder) to useemit_enum_struct_variantandemit_tuple_struct.
The current behavior feels like an oversight, and (2) would allow for finer-grained control over the serialization and nicer JSON. If people agree that this should happen before 1.0, and that (2) is the better design, I'd be interested in tackling this as my first rustc contribution.