Skip to content

Commit

Permalink
Merge pull request #99 from Peternator7/peternator7/enum_count
Browse files Browse the repository at this point in the history
Replaced const fn count with associated constant.
  • Loading branch information
Peternator7 authored Jul 26, 2020
2 parents 95645ca + ec32972 commit ee8a0d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
8 changes: 4 additions & 4 deletions strum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ pub trait EnumMessage {
/// }
/// ```
pub trait EnumProperty {
fn get_str(&self, &str) -> Option<&'static str>;
fn get_int(&self, &str) -> Option<usize> {
fn get_str(&self, prop: &str) -> Option<&'static str>;
fn get_int(&self, _prop: &str) -> Option<usize> {
Option::None
}

fn get_bool(&self, &str) -> Option<bool> {
fn get_bool(&self, _prop: &str) -> Option<bool> {
Option::None
}
}
Expand All @@ -215,7 +215,7 @@ where
/// A trait for capturing the number of variants in Enum. This trait can be autoderived by
/// `strum_macros`.
pub trait EnumCount {
fn count() -> usize;
const COUNT: usize;
}

/// A trait for retrieving the names of each variant in Enum. This trait can
Expand Down
19 changes: 5 additions & 14 deletions strum_macros/src/macros/enum_count.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::{Span, TokenStream};
use proc_macro2::TokenStream;
use syn;

pub(crate) fn enum_count_inner(ast: &syn::DeriveInput) -> TokenStream {
Expand All @@ -9,23 +9,14 @@ pub(crate) fn enum_count_inner(ast: &syn::DeriveInput) -> TokenStream {

// Used in the quasi-quotation below as `#name`
let name = &ast.ident;
let const_name = &syn::Ident::new(
&format!("{}_COUNT", name.to_string().to_uppercase()),
Span::call_site(),
);

// Helper is provided for handling complex generic types correctly and effortlessly
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

quote! {
// Implementation
impl #impl_generics ::strum::EnumCount for #name #ty_generics #where_clause {
fn count() -> usize {
#n
}
}

#[allow(dead_code, missing_docs)]
pub const #const_name: usize = #n;
// Implementation
impl #impl_generics ::strum::EnumCount for #name #ty_generics #where_clause {
const COUNT: usize = #n;
}
}
}
5 changes: 2 additions & 3 deletions strum_tests/tests/enum_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ enum Week {

#[test]
fn simple_test() {
assert_eq!(7, Week::count());
assert_eq!(Week::count(), WEEK_COUNT);
assert_eq!(Week::iter().count(), WEEK_COUNT);
assert_eq!(7, Week::COUNT);
assert_eq!(Week::iter().count(), Week::COUNT);
}

0 comments on commit ee8a0d2

Please sign in to comment.