#enums #macro-derive #variant #count #derive

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

#1507 in Rust patterns

Download history 28114/week @ 2024-07-19 30555/week @ 2024-07-26 28020/week @ 2024-08-02 28788/week @ 2024-08-09 35963/week @ 2024-08-16 38769/week @ 2024-08-23 32530/week @ 2024-08-30 32718/week @ 2024-09-06 32437/week @ 2024-09-13 30252/week @ 2024-09-20 33442/week @ 2024-09-27 32260/week @ 2024-10-04 29747/week @ 2024-10-11 36318/week @ 2024-10-18 30446/week @ 2024-10-25 25806/week @ 2024-11-01

130,150 downloads per month
Used in 228 crates (12 directly)

MIT license

4KB

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

~1.5MB
~37K SLoC