You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
enumFlags{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.
The text was updated successfully, but these errors were encountered:
Vec::deserialize is not magic. You should have no trouble adapting the implementation to insert directly into a BitFlags instead of going through the Vec.
Attempting to parse both an integer and a sequence could make sense, but there's no equivalent for serializing – you have to make a choice.
If you want to submit a PR with a module for use with #[serde(with = "...")], I'll happily merge that.
Here's a thought, something that happens often in practice: you're deserializing something that looks like
into your own bitflags format which looks like
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
orArrayVec<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.The text was updated successfully, but these errors were encountered: