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

#1604 in Rust patterns

Download history 24747/week @ 2024-03-14 28843/week @ 2024-03-21 26812/week @ 2024-03-28 17315/week @ 2024-04-04 19747/week @ 2024-04-11 22865/week @ 2024-04-18 19322/week @ 2024-04-25 19328/week @ 2024-05-02 20155/week @ 2024-05-09 24547/week @ 2024-05-16 24053/week @ 2024-05-23 29571/week @ 2024-05-30 34850/week @ 2024-06-06 28849/week @ 2024-06-13 30421/week @ 2024-06-20 27535/week @ 2024-06-27

127,958 downloads per month
Used in 227 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
~35K SLoC