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 EnumCount derive #15

Closed
Boscop opened this issue Mar 7, 2018 · 2 comments · Fixed by #37
Closed

Add EnumCount derive #15

Boscop opened this issue Mar 7, 2018 · 2 comments · Fixed by #37

Comments

@Boscop
Copy link

Boscop commented Mar 7, 2018

I wrote this proc-macro because I needed it (for mapping Enum cases from different enums to Vec regions, making the Vec len equal to the combined number of all cases) and I think it would make sense to include it in this crate, because it already contains many useful derives for enums.
https://github.com/Boscop/enum-count/blob/master/src/lib.rs
https://github.com/Boscop/enum-count-derive/blob/master/src/lib.rs

It allows stuff like this:

#[derive(EnumCount)] enum Aa { }

#[derive(EnumCount)]
enum Bb { B0 = AA_COUNT as _ }

#[repr(u8)]
#[derive(EnumCount)]
enum Cc { C0 = AA_COUNT as _, C1 = BB_COUNT as _ }

#[repr(usize)]
#[derive(EnumCount)]
enum Dd { D0 = AA_COUNT, D1 = BB_COUNT, D2 = CC_COUNT }

fn main() {
	println!("{} {} {} {}", Aa::count(), BB_COUNT, Cc::count(), DD_COUNT);
}

(The constant is necessary for it to be used in other enums and static array lens etc.)

@Peternator7
Copy link
Owner

This makes sense to me. Would you be willing to make the changes and submit a PR?

Once rust-lang/rfcs#911 is stablized, we can make count() const as well.

@Dushistov
Copy link
Contributor

I also need this feature too. I will try to make PR.

Dushistov added a commit to Dushistov/strum that referenced this issue Oct 29, 2018
Peternator7 pushed a commit that referenced this issue Nov 5, 2018
* feature: add enum count macros

fix #15

* add docs for EnumCount
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

Successfully merging a pull request may close this issue.

3 participants