{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/19756","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/19756/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/19756/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/19756/events","html_url":"https://github.com/rust-lang/rust/issues/19756","id":51739409,"number":19756,"title":"deriving(Encodable) does not use emit_enum_struct_variant or emit_tuple_struct","user":{"login":"emk","id":36963,"avatar_url":"https://avatars.githubusercontent.com/u/36963?v=3","gravatar_id":"","url":"https://api.github.com/users/emk","html_url":"https://github.com/emk","followers_url":"https://api.github.com/users/emk/followers","following_url":"https://api.github.com/users/emk/following{/other_user}","gists_url":"https://api.github.com/users/emk/gists{/gist_id}","starred_url":"https://api.github.com/users/emk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emk/subscriptions","organizations_url":"https://api.github.com/users/emk/orgs","repos_url":"https://api.github.com/users/emk/repos","events_url":"https://api.github.com/users/emk/events{/privacy}","received_events_url":"https://api.github.com/users/emk/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rust-lang/rust/labels/A-syntaxext","name":"A-syntaxext","color":"d7e102"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2014-12-11T21:20:33Z","updated_at":"2015-01-27T19:23:56Z","closed_at":"2015-01-27T17:56:26Z","body":"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.\r\n\r\nCurrently, the code which derives `Encodable` has a few weird design decisions:\r\n\r\n**1. Rust always uses `emit_enum_variant` instead of `emit_enum_struct_variant`.**\r\n\r\nIf we have a structure `ExEnum` as follows:\r\n\r\n```rust\r\n#[deriving(Encodable)]\r\nenum ExEnum { Foo, Bar(f64), Baz{x: f64, y: f64} }\r\n```\r\n\r\n…then both `Bar` and `Baz` will emit code to call `emit_enum_variant`. For example, JSON encoding will yield:\r\n\r\n```\r\n\"Foo\"\r\n{\"variant\":\"Bar\",\"fields\":[1]}\r\n{\"variant\":\"Baz\",\"fields\":[1,2]}\r\n```\r\n\r\nThis would probably look a lot nicer with `x:` and `y:` in the `Baz` case.\r\n\r\n**2. Rust will always use `emit_struct` instead of `emit_tuple_struct`**\r\n\r\n```rust\r\n#[deriving(Encodable)]\r\nstruct ExTupleStruct(f64);\r\n```\r\n\r\nThis will encode to JSON as:\r\n\r\n```\r\n{\"_field0\":1}\r\n```\r\n\r\nThis is particularly unfortunate—the resulting data would look nicer if it went through `emit_tuple_struct` and used some sort of array-based encoding.\r\n\r\n**Proposed fixes**\r\n\r\nI can see two ways forward:\r\n\r\n- Option 1: Officially drop `emit_enum_struct_variant` and `emit_tuple_struct` and always use `emit_enum_variant` and `emit_struct`.\r\n- Option 2: Fix the `Encoder` (or the new `RustcEncoder`) to use `emit_enum_struct_variant` and `emit_tuple_struct`.\r\n\r\nThe 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. "},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/71710319","html_url":"https://github.com/rust-lang/rust/issues/19756#issuecomment-71710319","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/19756","id":71710319,"user":{"login":"steveklabnik","id":27786,"avatar_url":"https://avatars.githubusercontent.com/u/27786?v=3","gravatar_id":"","url":"https://api.github.com/users/steveklabnik","html_url":"https://github.com/steveklabnik","followers_url":"https://api.github.com/users/steveklabnik/followers","following_url":"https://api.github.com/users/steveklabnik/following{/other_user}","gists_url":"https://api.github.com/users/steveklabnik/gists{/gist_id}","starred_url":"https://api.github.com/users/steveklabnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/steveklabnik/subscriptions","organizations_url":"https://api.github.com/users/steveklabnik/orgs","repos_url":"https://api.github.com/users/steveklabnik/repos","events_url":"https://api.github.com/users/steveklabnik/events{/privacy}","received_events_url":"https://api.github.com/users/steveklabnik/received_events","type":"User","site_admin":false},"created_at":"2015-01-27T19:23:56Z","updated_at":"2015-01-27T19:23:56Z","body":"The in-tree one is private to Rust, so you don't need to stress out over it :heart: "}}