#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

#18 in #ordinal

Download history 60/week @ 2024-04-01 17/week @ 2024-04-08 22/week @ 2024-04-15 17/week @ 2024-04-22 10/week @ 2024-04-29 22/week @ 2024-05-06 23/week @ 2024-05-13 29/week @ 2024-05-20 28/week @ 2024-05-27 23/week @ 2024-06-03 21/week @ 2024-06-10 31/week @ 2024-06-17 30/week @ 2024-06-24 3/week @ 2024-07-01 97/week @ 2024-07-08 56/week @ 2024-07-15

189 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
~36K SLoC