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

Add RangeEnum trait #10418

Closed
orenbenkiki opened this issue Nov 11, 2013 · 7 comments
Closed

Add RangeEnum trait #10418

orenbenkiki opened this issue Nov 11, 2013 · 7 comments

Comments

@orenbenkiki
Copy link

Following the last few comments on #4926.

Add a RangeEnum trait for simple Enums (which implement FromPrimitive) to allow quick access to the total number of values, and provide an iterator over the values.

@nikomatsakis
Copy link
Contributor

One thought I had in an e-mail conversation with @martindemello was that it might be useful to make a trait for working with enums which allows conversion not from the discriminant of an enum but rather from its index in the list of variants (which is usually, but not always, equal to the discriminant). This would allow us to guarantee for example that we can compact use bits from 0...n-1 to represent the values of an enum, and it would make it easier to define things like count() (total number of variants) and so forth. This might or might not be RangeEnum.

@sfackler
Copy link
Member

Java defines the ordinal() method on enums to return the index of a variant. I find it dangerous as code using it is extremely fragile to modifications of the enum. It may be less dangerous here if it has to be manually specified to be derived, as that can serve as documentation that you need to be careful when modifying the enum. People might miss it if it's buried inside of Eq, Decodable, Encodable, Ord, ... though.

@nikomatsakis
Copy link
Contributor

I'd rather say the onus is on the caller of ordinal() to use it in a context where order doesn't matter (e.g., an EnumSet).

@martindemello
Copy link
Contributor

I think Ordinal would be a better trait name than RangeEnum

@nikomatsakis
Copy link
Contributor

On Fri, Nov 15, 2013 at 10:34:02PM -0800, Martin DeMello wrote:

I think Ordinal would be a better trait name than RangeEnum

+1

@emberian
Copy link
Member

This is still relevant and useful. Should this be baked into the compiler somehow?

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#824

flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 10, 2023
Run dogfood to completion

Run dogfood on all packages before failing the test. Failing early is painful on lints which trigger on multiple crates.

changelog: None
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

6 participants