Repository: Peternator7/strum (rust)

Issue:
Use associated constant for EnumCount 
Use an associated constant for EnumCount:
```rust
pub trait EnumCount {
    const COUNT: usize;
}
```
This would replace both the `count()` method
https://github.com/Peternator7/strum/blob/a20ee16d6bb0d26c999767e4a9dd7d8ef013c563/strum_macros/src/macros/enum_count.rs#L23
and the `<enum-name>_COUNT` constant
https://github.com/Peternator7/strum/blob/a20ee16d6bb0d26c999767e4a9dd7d8ef013c563/strum_macros/src/macros/enum_count.rs#L29
and it works on stable [since 1.20](https://blog.rust-lang.org/2017/08/31/Rust-1.20.html).

(There is no reason why this needs to be a method (not even a const-fn method), and it's unelegant to have both a method and a non-associated constant.)

An associated constant is the right way to express this.

Related discussion: https://github.com/Peternator7/strum/issues/58 https://github.com/Peternator7/strum/issues/15#issuecomment-372063212

---

Btw, this was motivated by my real-world use case that is currently not possible with `EnumCount`, I need to define the following:
```rust

pub trait Param: From<usize> + Into<usize> + Serialize + DeserializeOwned {
	const PARAM_COUNT: usize;
}

impl<T: From<usize> + Into<usize> + EnumCount> Param for T
where
	[T; <T as EnumCount>::count()]: Serialize + DeserializeOwned,
{
	const PARAM_COUNT: usize = T::count();
}
```
It's not possible because `count()` is not a constant.

Pull request:
Replaced const fn count with associated constant.
Fixes #96 

Patch:
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<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
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;
+        }
     }
 }


Test patch:
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);
 }