explicit-discriminant

Crates.ioexplicit-discriminant
lib.rsexplicit-discriminant
version0.1.4
sourcesrc
created_at2023-08-16 16:12:49.836243
updated_at2023-08-16 21:42:53.501575
descriptionMacro for enforcing enum discriminants
homepagehttps://github.com/rcrisanti/explicit-discriminant
repositoryhttps://github.com/rcrisanti/explicit-discriminant
max_upload_size
id946109
size11,619
Ryan Crisanti (rcrisanti)

documentation

README

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,
}
Commit count: 29

cargo fmt