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

Replaced const fn count with associated constant. #99

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}