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

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 18/week @ 2024-11-13 24/week @ 2024-11-20 27/week @ 2024-11-27 38/week @ 2024-12-04 51/week @ 2024-12-11 13/week @ 2024-12-18 10/week @ 2025-01-01 17/week @ 2025-01-08 31/week @ 2025-01-15 15/week @ 2025-01-22 9/week @ 2025-01-29 41/week @ 2025-02-05 14/week @ 2025-02-12 22/week @ 2025-02-19 26/week @ 2025-02-26

105 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
~39K SLoC