#enums #discriminant #pattern #macro

macro explicit-discriminant

Macro for enforcing enum discriminants

4 releases

0.1.4 Aug 16, 2023
0.1.3 Aug 16, 2023
0.1.2 Aug 16, 2023
0.1.0 Aug 16, 2023

#2236 in Data structures

Download history 124/week @ 2024-08-31 221/week @ 2024-09-07 84/week @ 2024-09-14 293/week @ 2024-09-21 1/week @ 2024-09-28 223/week @ 2024-10-05 73/week @ 2024-10-12 188/week @ 2024-10-19 89/week @ 2024-10-26 55/week @ 2024-11-02 7/week @ 2024-11-09 22/week @ 2024-11-16 27/week @ 2024-11-23 1041/week @ 2024-11-30 176/week @ 2024-12-07 163/week @ 2024-12-14

1,407 downloads per month

Custom license

8KB
114 lines

explicit-discriminant

CI CD

This crate enforces explicit discriminants of an enum.

use explicit_discriminant::ExplicitDiscriminant;

#[derive(ExplicitDiscriminant)]
enum MyEnum {
    One = 1,
    Two = 2,
    Three, // will not compile
    Four = 4
}

You can also optionally add patterns that will enforce which discriminant values are allowed.

use explicit_discriminant::ExplicitDiscriminant;

#[derive(ExplicitDiscriminant)]
#[pattern(2..=3 | 12..)]
#[pattern(9 | ..-3)]
enum MyEnum {
    MinusFour = -4,
    One = 1, // error here, since not in any of the patterns
    Two = 2,
    Three = 3, 
    Nine = 9,
    Ten = 10, // error here, since not in any of the patterns
    Twelve = 12,
    OneHundred = 100,
}

Dependencies

~275–730KB
~17K SLoC