Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating strong ordinal values independent of discriminants #235

Open
fralalonde opened this issue Aug 25, 2022 · 0 comments
Open

Generating strong ordinal values independent of discriminants #235

fralalonde opened this issue Aug 25, 2022 · 0 comments

Comments

@fralalonde
Copy link

fralalonde commented Aug 25, 2022

[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 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant