#enums #variant #macro #derive #definition #compile #const-enum-tools

nightly macro const-enum-tools-derive

Trait definitions for working with enums at compile time using macros

7 unstable releases (3 breaking)

0.4.3 May 3, 2022
0.4.2 May 3, 2022
0.3.2 May 2, 2022
0.2.2 May 2, 2022
0.1.0 May 1, 2022

#252 in #variant

MIT license

11KB
153 lines

const-enum-tools-derive

Derive macros for const-enum-tools.

#[derive(VariantCount)] on an enum adds to it a constant that has the number of variants of the enum.

#[derive(VariantList)] on an enum implements a method that gets the enum variant's index and an associated constant on it that has the name of all the variants. This allows you to iterate over the variants, as well as get the name of a variant you have as a string.

In cases where the discriminant of an enum variant corresponds to its index, .variant_index() will include an unsafe block that effectively copies the value's underlying bytes in order to clone them. This seems to be safe for now, but if any unsafety is found to leak through it will be removed. This optimization can be disabled by placing #[disallow_instance_bitcopy] on an enum variant or the enum itself.

Dependencies

~1.5MB
~34K SLoC