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
Context: I'm developing an enum-indexed structure that is similar to a HashMap<Enum, V> but that is backed by an array of size Enum::COUNT (where COUNT is strum-generated EnumCount). Indexing currently uses the enum's discriminant (as usize) which is brittle because discriminants outside of the array's indexing range could inadvertently be assigned to values of the Enum, breaking hell loose.
While strum's enum_iter() could be used to work with ordinal values, it would incur O(n) performance, negating the speed advantage of array indexing. I could not find any other strum mechanism of help, but maybe I overlooked a feature.
To fix this, I am thinking of developing a macro generating an fn ordinal(&self) -> usize for an Enum where returned index is guaranteed between 0..Enum::COUNT-1. The reciprocal fn from_ordinal(usize) -> E is not required but could easily be generated too.
Is this mechanism something that you believe would be within strum's scope - i.e. should I make a PR out of it? Would the EnumIndexArray generation also be of interest?
The text was updated successfully, but these errors were encountered:
[Enhancement proposal]
Context: I'm developing an enum-indexed structure that is similar to a HashMap<Enum, V> but that is backed by an array of size Enum::COUNT (where COUNT is strum-generated EnumCount). Indexing currently uses the enum's discriminant (as usize) which is brittle because discriminants outside of the array's indexing range could inadvertently be assigned to values of the Enum, breaking hell loose.
While strum's
enum_iter()
could be used to work with ordinal values, it would incur O(n) performance, negating the speed advantage of array indexing. I could not find any other strum mechanism of help, but maybe I overlooked a feature.To fix this, I am thinking of developing a macro generating an
fn ordinal(&self) -> usize
for an Enum where returned index is guaranteed between 0..Enum::COUNT-1. The reciprocalfn from_ordinal(usize) -> E
is not required but could easily be generated too.Is this mechanism something that you believe would be within strum's scope - i.e. should I make a PR out of it? Would the
EnumIndexArray
generation also be of interest?The text was updated successfully, but these errors were encountered: