1
0
mirror of https://github.com/danog/strum.git synced 2024-11-26 12:04:38 +01:00

Replaced const fn count with associated constant.

This commit is contained in:
Peter Glotfelty 🚀 2020-07-26 04:41:15 +00:00
parent 95645cac52
commit ec3297242c
3 changed files with 11 additions and 21 deletions

View File

@ -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
}
}
@ -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

View File

@ -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 {
@ -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;
}
}
}

View File

@ -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);
}