#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

#2205 in Data structures

Download history 229/week @ 2024-07-22 170/week @ 2024-07-29 158/week @ 2024-08-05 249/week @ 2024-08-12 198/week @ 2024-08-19 146/week @ 2024-08-26 118/week @ 2024-09-02 223/week @ 2024-09-09 85/week @ 2024-09-16 290/week @ 2024-09-23 226/week @ 2024-10-07 73/week @ 2024-10-14 185/week @ 2024-10-21 95/week @ 2024-10-28 51/week @ 2024-11-04

405 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