From ec3297242c75a5d47945eeff0dc2ebef09eb7e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Glotfelty=20=F0=9F=9A=80?= Date: Sun, 26 Jul 2020 04:41:15 +0000 Subject: [PATCH] Replaced const fn count with associated constant. --- strum/src/lib.rs | 8 ++++---- strum_macros/src/macros/enum_count.rs | 19 +++++-------------- strum_tests/tests/enum_count.rs | 5 ++--- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/strum/src/lib.rs b/strum/src/lib.rs index 4623eb2..0f00094 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -192,12 +192,12 @@ pub trait EnumMessage { /// } /// ``` pub trait EnumProperty { - fn get_str(&self, &str) -> Option<&'static str>; - fn get_int(&self, &str) -> Option { + fn get_str(&self, prop: &str) -> Option<&'static str>; + fn get_int(&self, _prop: &str) -> Option { Option::None } - fn get_bool(&self, &str) -> Option { + fn get_bool(&self, _prop: &str) -> Option { 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 diff --git a/strum_macros/src/macros/enum_count.rs b/strum_macros/src/macros/enum_count.rs index c22cfa5..0a26b62 100644 --- a/strum_macros/src/macros/enum_count.rs +++ b/strum_macros/src/macros/enum_count.rs @@ -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; + } } } diff --git a/strum_tests/tests/enum_count.rs b/strum_tests/tests/enum_count.rs index bdb5b74..895fab6 100644 --- a/strum_tests/tests/enum_count.rs +++ b/strum_tests/tests/enum_count.rs @@ -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); }