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

#1681 in Rust patterns

Download history 25917/week @ 2024-01-05 26324/week @ 2024-01-12 29467/week @ 2024-01-19 27848/week @ 2024-01-26 24468/week @ 2024-02-02 23623/week @ 2024-02-09 23294/week @ 2024-02-16 22997/week @ 2024-02-23 19673/week @ 2024-03-01 20755/week @ 2024-03-08 26319/week @ 2024-03-15 33145/week @ 2024-03-22 20832/week @ 2024-03-29 17035/week @ 2024-04-05 19532/week @ 2024-04-12 19686/week @ 2024-04-19

86,871 downloads per month
Used in 229 crates (13 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
~33K SLoC