diff --git a/strum/src/lib.rs b/strum/src/lib.rs index fb36882f..7254479c 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -221,7 +221,8 @@ pub trait EnumCount { /// A trait for retrieving the names of each variant in Enum. This trait can /// be autoderived by `strum_macros`. pub trait VariantNames { - fn variants() -> &'static [&'static str]; + /// Names of the variants of this enum + const VARIANTS: &'static [&'static str]; } #[cfg(feature = "derive")] diff --git a/strum_macros/src/macros/enum_variant_names.rs b/strum_macros/src/macros/enum_variant_names.rs index 0bc550e2..ae6dcd7e 100644 --- a/strum_macros/src/macros/enum_variant_names.rs +++ b/strum_macros/src/macros/enum_variant_names.rs @@ -26,13 +26,7 @@ pub fn enum_variant_names_inner(ast: &syn::DeriveInput) -> TokenStream { quote! { impl #impl_generics ::strum::VariantNames for #name #ty_generics #where_clause { - /// Return a slice containing the names of the variants of this enum - #[allow(dead_code)] - fn variants() -> &'static [&'static str] { - &[ - #(#names),* - ] - } + const VARIANTS: &'static [&'static str] = &[ #(#names),* ]; } } } diff --git a/strum_tests/tests/enum_variant_names.rs b/strum_tests/tests/enum_variant_names.rs index f5f78ceb..37d8758c 100644 --- a/strum_tests/tests/enum_variant_names.rs +++ b/strum_tests/tests/enum_variant_names.rs @@ -15,7 +15,7 @@ fn simple() { Yellow, } - assert_eq!(&Color::variants(), &["Red", "Blue", "Yellow"]); + assert_eq!(Color::VARIANTS, &["Red", "Blue", "Yellow"]); } #[test] @@ -29,7 +29,7 @@ fn variant_names_trait() { } fn generic_function() { - assert_eq!(T::variants(), &["Red", "Blue", "Yellow"]); + assert_eq!(T::VARIANTS, &["Red", "Blue", "Yellow"]); } generic_function::(); @@ -47,10 +47,7 @@ fn plain_kebab() { RebeccaPurple, } - assert_eq!( - &Color::variants(), - &["red", "blue", "yellow", "rebecca-purple"] - ); + assert_eq!(Color::VARIANTS, &["red", "blue", "yellow", "rebecca-purple"]); } #[test] @@ -66,7 +63,7 @@ fn non_plain_camel() { } assert_eq!( - &Color::variants(), + Color::VARIANTS, &["deep-pink", "green-yellow", "cornflower-blue", "other"] ); } @@ -83,14 +80,14 @@ fn clap_and_structopt() { } assert_eq!( - &Color::variants(), + Color::VARIANTS, &["red", "blue", "yellow", "rebecca-purple"] ); let _clap_example = clap::App::new("app").arg( clap::Arg::with_name("color") .long("color") - .possible_values(Color::variants()) + .possible_values(Color::VARIANTS) .case_insensitive(true), ); @@ -101,7 +98,7 @@ fn clap_and_structopt() { #[structopt( long = "color", default_value = "Color::Blue", - raw(possible_values = "Color::variants()") + raw(possible_values = "Color::VARIANTS") )] color: Color, }