Skip to content

Way to support alternative serde format + being able to access the number of variants #43

@aldanor

Description

@aldanor

Here's a thought, something that happens often in practice: you're deserializing something that looks like

{"flags": ["foo", "baz"]}

into your own bitflags format which looks like

enum Flags { Foo, Bar, Baz }

There's a few ways to do this manually, but wouldn't it be nice if this crate provided some way to do it? Not sure what's the best way, since making it a crate feature would immediately change it for all wrapped enums, whereas it's more of a runtime thing. Perhaps the deserializer could try parsing a sequence if it can't parse an integer? There shouldn't be any ambiguity there. Or maybe a separate public module that can be used with #[serde(with)] - this way some fields can be (de)serialized as ints and some - as sequences.

Also, if doing it by hand and in order to avoid allocations, you might need something like T::N_VARIANTS or ArrayVec<T, T::N_VARIANTS> (if you're sure the flags don't repeat...) so you can instantiate something like [T; T::N_VARIANTS] which currently is not something exposed by any of the traits unless I'm missing something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions