#macro-derive #enums #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

#1626 in Rust patterns

Download history 30794/week @ 2024-08-11 36467/week @ 2024-08-18 38342/week @ 2024-08-25 32470/week @ 2024-09-01 33688/week @ 2024-09-08 31101/week @ 2024-09-15 30119/week @ 2024-09-22 33914/week @ 2024-09-29 31400/week @ 2024-10-06 31788/week @ 2024-10-13 34918/week @ 2024-10-20 30164/week @ 2024-10-27 23978/week @ 2024-11-03 25267/week @ 2024-11-10 28726/week @ 2024-11-17 24212/week @ 2024-11-24

103,409 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