#enum #derive #variant #count

macro variant_count

Derive macro for enum which adds to it the count of variants

2 stable releases

1.1.0 Apr 7, 2021
1.0.0 Nov 1, 2018

#138 in Rust patterns

Download history 238/week @ 2020-12-23 69/week @ 2020-12-30 300/week @ 2021-01-06 592/week @ 2021-01-13 251/week @ 2021-01-20 266/week @ 2021-01-27 253/week @ 2021-02-03 183/week @ 2021-02-10 718/week @ 2021-02-17 492/week @ 2021-02-24 419/week @ 2021-03-03 375/week @ 2021-03-10 432/week @ 2021-03-17 461/week @ 2021-03-24 140/week @ 2021-03-31 1065/week @ 2021-04-07

1,626 downloads per month
Used in 3 crates (2 directly)

MIT license

3KB

Enum variant count

Crates.io Docs

This crate provides VariantCount derive macro for enum which adds to it the VARIANT_COUNT constant, containing count of enum variants.

The VariantCount usage example:

#[derive(VariantCount)]
enum Test {
    First(i32),
    Second(Option<String>),
    Third,
}

assert_eq!(Test::VARIANT_COUNT, 3);

Usage

If you're using Cargo, just add it to your Cargo.toml:

[dependencies]
variant_count = "1.1"

License

MIT


lib.rs:

This crate provides VariantCount derive macro for enum which adds to it the VARIANT_COUNT constant, containing count of enum variants.

Example

extern crate variant_count;

use variant_count::VariantCount;

#[derive(VariantCount)]
enum Test {
First(i32),
Second(Option<String>),
Third,
}

assert_eq!(Test::VARIANT_COUNT, 3);

Usage

If you're using Cargo, just add it to your Cargo.toml:

[dependencies]
variant_count = "1.1"

Use VariantCount in the derive enum attribute. !

Dependencies

~355–780KB
~19K SLoC