Rustc-serialize emmitting enum struct variant as regular variant


#1

While implementing rustc-serialize for a binary format (part of neo4j graph db new binary protocol) I noticed that serialization of enum struct variants doens’t call the right methods on the Encoder trait, instead the methods for regular enum variants are called:

// given the following enum
// EDIT: added annotation
#[derive(RustcEncodable)]
enum MyEnum {
    Variant { field1: u16, field2: u16 },
}

impl Encoder for MyEncoder {
    // this method should be called, but it isn't
    fn emit_enum_struct_variant<F>(...) { ... }

    // instead, this method is called
    fn emit_enum_variant<F>(...) { ... }
}

Is this supposed to happen?


#2

Sounds like a bug, maybe? Struct variants don’t seem to be used a whole lot because they’re kinda clunky so maybe no one has encountered this before. I would submit an issue to the repo:


#3

It looks like the problem is in the #[derive(RustcEncodable)] (which I forgot the type in my example), meaning this is more likely to be an issue with rust itself than rustc-serialize.

I’ll check the json serialization to see if this also happens there.
EDIT: rustc_serialize::json intentionally serializes enum struct variants as regular variants.


#4

I think this is known, here’s an open issue in rust: https://github.com/rust-lang/rust/issues/19756