Skip to content

Derive permits internally tagged enum with tuple variants, errors at runtime #1307

Open
@abonander

Description

@abonander

The Enum Representations page specifies the following:

Using a #[serde(tag = "...")] attribute on an enum containing a tuple variant is an error at compile time.

I take that to mean that the following will produce a compile-time error, because there's not really a good way to format internally tagged enums with tuple variants:

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Foo {
    Bar(String),
    Baz(String),
}

However, it turns out that this only becomes an error at runtime, and only when serializing the specific variant: http://play.rust-lang.org/?gist=9f5656641805cd794d79ef715bcf850e&version=stable&mode=debug

Deserialization, on the other hand, just produces a hugely unhelpful error if you try something like { "type": "Foo", "__field0": "some string" }:

invalid type: map, expected a string

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions