#enum-variant #enums #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

#1234 in Rust patterns

Download history 9038/week @ 2024-12-25 18282/week @ 2025-01-01 23699/week @ 2025-01-08 26314/week @ 2025-01-15 26411/week @ 2025-01-22 27981/week @ 2025-01-29 30929/week @ 2025-02-05 34838/week @ 2025-02-12 199989/week @ 2025-02-19 31975/week @ 2025-02-26 29928/week @ 2025-03-05 28245/week @ 2025-03-12 28997/week @ 2025-03-19 29728/week @ 2025-03-26 29981/week @ 2025-04-02 25890/week @ 2025-04-09

119,558 downloads per month
Used in 228 crates (12 directly)

MIT license

4KB

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. !


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

Dependencies

~1.5MB
~38K SLoC