const_enum_tools/
lib.rs

1#![feature(generic_const_exprs)]
2
3/// An enum trait that shows a count of its variants.
4pub trait VariantCount {
5    const VARIANT_COUNT: usize;
6}
7
8/// An enum trait that allows getting the index of its variant
9/// and thereby the name of the variant.
10pub trait VariantList where
11    Self: VariantCount,
12    [(); Self::VARIANT_COUNT]:,
13{
14
15    fn variant_index (&self) -> usize;
16
17    const VARIANTS: [&'static str; Self::VARIANT_COUNT];
18
19    fn variant_name (&self) -> String {
20        Self::VARIANTS[self.variant_index()].to_string()
21    }
22}