#ordinal #enums #variant

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

#21 in #ordinal

Download history 52/week @ 2024-07-29 23/week @ 2024-08-05 24/week @ 2024-08-12 13/week @ 2024-08-19 51/week @ 2024-08-26 24/week @ 2024-09-02 20/week @ 2024-09-09 24/week @ 2024-09-16 43/week @ 2024-09-23 32/week @ 2024-09-30 1/week @ 2024-10-07 23/week @ 2024-10-14 13/week @ 2024-10-21 23/week @ 2024-10-28 44/week @ 2024-11-04 18/week @ 2024-11-11

100 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
~37K SLoC