Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move EnumVariantsNames into a trait which the macro implements #74

Merged
merged 2 commits into from Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion strum/src/lib.rs
Expand Up @@ -40,7 +40,7 @@
//! | [Display] | Converts enum variants to strings |
//! | [AsRefStr] | Converts enum variants to `&'static str` |
//! | [IntoStaticStr] | Implements `From<MyEnum> for &'static str` on an enum |
//! | [EnumVariantNames] | Adds a `variants` method returning an array of discriminant names |
//! | [EnumVariantNames] | Implements Strum::VariantNames which adds a static `variants` method returning an array of discriminant names |
//! | [EnumIter] | Creates a new type that iterates of the variants of an enum. |
//! | [EnumProperty] | Add custom properties to enum variants. |
//! | [EnumMessage] | Add a verbose message to an enum variant. |
Expand Down Expand Up @@ -218,6 +218,12 @@ pub trait EnumCount {
fn count() -> usize;
}

/// 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];
}

#[cfg(feature = "derive")]
#[allow(unused_imports)]
#[macro_use]
Expand Down
7 changes: 3 additions & 4 deletions strum_macros/src/macros/enum_variant_names.rs
@@ -1,8 +1,7 @@
use proc_macro2::TokenStream;
use syn;

use crate::helpers::case_style::CaseStyle;
use crate::helpers::{extract_meta, CaseStyleHelpers, MetaIteratorHelpers};
use crate::helpers::{case_style::CaseStyle, extract_meta, CaseStyleHelpers, MetaIteratorHelpers};

pub fn enum_variant_names_inner(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
Expand All @@ -24,10 +23,10 @@ pub fn enum_variant_names_inner(ast: &syn::DeriveInput) -> TokenStream {
.collect::<Vec<_>>();

quote! {
impl #name {
impl VariantNames for #name {
Spanfile marked this conversation as resolved.
Show resolved Hide resolved
/// Return a slice containing the names of the variants of this enum
#[allow(dead_code)]
pub fn variants() -> &'static [&'static str] {
fn variants() -> &'static [&'static str] {
&[
#(#names),*
]
Expand Down
18 changes: 18 additions & 0 deletions strum_tests/tests/enum_variant_names.rs
Expand Up @@ -3,6 +3,7 @@ extern crate strum_macros;
#[macro_use]
extern crate structopt;
extern crate strum;
use strum::VariantNames;

#[test]
fn simple() {
Expand All @@ -17,6 +18,23 @@ fn simple() {
assert_eq!(&Color::variants(), &["Red", "Blue", "Yellow"]);
}

#[test]
fn variant_names_trait() {
#[allow(dead_code)]
#[derive(EnumVariantNames)]
enum Color {
Red,
Blue,
Yellow,
}

fn generic_function<T: VariantNames>() {
assert_eq!(T::variants(), &["Red", "Blue", "Yellow"]);
}

generic_function::<Color>();
}

#[test]
fn plain_kebab() {
#[allow(dead_code)]
Expand Down