#enums #ordinal #variant #fields #definition #index

macro ordinalizer

Derive an ordinal function for enums which returns the index of a variant in the enum definition

1 unstable release

0.1.0 May 6, 2020

#14 in #ordinal

Download history 10/week @ 2024-01-08 8/week @ 2024-01-29 5/week @ 2024-02-12 31/week @ 2024-02-19 48/week @ 2024-02-26 46/week @ 2024-03-04 33/week @ 2024-03-11 22/week @ 2024-03-18 24/week @ 2024-03-25 60/week @ 2024-04-01 17/week @ 2024-04-08 22/week @ 2024-04-15 17/week @ 2024-04-22

119 downloads per month
Used in 3 crates

MIT/Apache

7KB
73 lines

A simple derive macro to generate an ordinal() method for enums.

Unlike num_derive::ToPrimitive, this derive macro allows non-C-like enums. The ordinal function reflects the variant of the enum and does not account for fields.

Example

use ordinalizer::Ordinal;
#[derive(Ordinal)]
enum Animal {
    Dog,
    Cat {
        age: i32,
    }
}

assert_eq!(Animal::Dog.ordinal(), 0);
assert_eq!((Animal::Cat { age: 10 }).ordinal(), 1);

Dependencies

~1.5MB
~34K SLoC