Skip to content

Deriving decodable is useless for struct with anonymous fields #15659

Closed
@tomaka

Description

@tomaka

For example:

#[deriving(Decodable)]
struct Duration(int);

The decodable implementation created here tries to decode an object with a field named _field0 and of type int, like { "_field0": 5 }

There two problems with this:

  • Most people won't expect this behavior (I would expect this code to decode a single int, not an object)
  • The fact that the field name is arbitrary makes this trait impl useless anyway

I think that the deriving implementation of decodable for structs with one anonymous field should be equivalent to decoding the type of the field itself.

Structs with multiple anonymous fields are ambiguous, so I think deriving decodable for them should simply trigger a compilation error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions