Repository: rust-phf/rust-phf (rust)

Issue:
`usize` not usable as literal key
```
error: unsupported key expression
     --> src\movements.rs:32592:5
      |
32592 |     14usize /* SpotId::Amagi_Breach__East_Entrance__East */ => &TIMES_WATER__AMAGI_BREACH__EAST_ENTRANCE__EAST,
      |     ^^^^^^^
```

#262 supposedly enabled usize for PhfHash, but it's not present in https://github.com/rust-phf/rust-phf/blob/master/phf_macros/src/lib.rs where I would assume it should be to allow use in `phf_map!`.

Pull request:
Allow isize/usize as key expressions in phf_macros
These types already implement `PhfHash` as of #262, but were not supported as valid key expressions in `phf_macros`.

Added a test case for both using the `target_pointer_width` cfg.

Also included a patch version bump, let me know if that's okay.

Fixes #313.

Patch:
diff --git a/phf/Cargo.toml b/phf/Cargo.toml
index c2d40a9..52aab12 100644
--- a/phf/Cargo.toml
+++ b/phf/Cargo.toml
@@ -23,7 +23,7 @@ unicase = ["phf_macros?/unicase", "phf_shared/unicase"]
 macros = ["phf_macros"]
 
 [dependencies]
-phf_macros = { version = "^0.11.2", optional = true, path = "../phf_macros" }
+phf_macros = { version = "^0.11.3", optional = true, path = "../phf_macros" }
 phf_shared = { version = "^0.11.2", default-features = false, path = "../phf_shared" }
 serde = { version = "1.0", optional = true }
 
diff --git a/phf_macros/Cargo.toml b/phf_macros/Cargo.toml
index 921d3e6..db28c09 100644
--- a/phf_macros/Cargo.toml
+++ b/phf_macros/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "phf_macros"
-version = "0.11.2"
+version = "0.11.3"
 authors = ["Steven Fackler <sfackler@gmail.com>"]
 edition = "2021"
 license = "MIT"
diff --git a/phf_macros/src/lib.rs b/phf_macros/src/lib.rs
index a033036..e942a9e 100644
--- a/phf_macros/src/lib.rs
+++ b/phf_macros/src/lib.rs
@@ -25,11 +25,13 @@ enum ParsedKey {
     I32(i32),
     I64(i64),
     I128(i128),
+    Isize(isize),
     U8(u8),
     U16(u16),
     U32(u32),
     U64(u64),
     U128(u128),
+    Usize(usize),
     Bool(bool),
     #[cfg(feature = "unicase")]
     UniCase(UniCase<String>),
@@ -49,11 +51,13 @@ impl PhfHash for ParsedKey {
             ParsedKey::I32(s) => s.phf_hash(state),
             ParsedKey::I64(s) => s.phf_hash(state),
             ParsedKey::I128(s) => s.phf_hash(state),
+            ParsedKey::Isize(s) => s.phf_hash(state),
             ParsedKey::U8(s) => s.phf_hash(state),
             ParsedKey::U16(s) => s.phf_hash(state),
             ParsedKey::U32(s) => s.phf_hash(state),
             ParsedKey::U64(s) => s.phf_hash(state),
             ParsedKey::U128(s) => s.phf_hash(state),
+            ParsedKey::Usize(s) => s.phf_hash(state),
             ParsedKey::Bool(s) => s.phf_hash(state),
             #[cfg(feature = "unicase")]
             ParsedKey::UniCase(s) => s.phf_hash(state),
@@ -78,11 +82,13 @@ impl ParsedKey {
                     "i32" => Some(ParsedKey::I32(s.base10_parse::<u32>().unwrap() as i32)),
                     "i64" => Some(ParsedKey::I64(s.base10_parse::<u64>().unwrap() as i64)),
                     "i128" => Some(ParsedKey::I128(s.base10_parse::<u128>().unwrap() as i128)),
+                    "isize" => Some(ParsedKey::Isize(s.base10_parse::<usize>().unwrap() as isize)),
                     "u8" => Some(ParsedKey::U8(s.base10_parse::<u8>().unwrap())),
                     "u16" => Some(ParsedKey::U16(s.base10_parse::<u16>().unwrap())),
                     "u32" => Some(ParsedKey::U32(s.base10_parse::<u32>().unwrap())),
                     "u64" => Some(ParsedKey::U64(s.base10_parse::<u64>().unwrap())),
                     "u128" => Some(ParsedKey::U128(s.base10_parse::<u128>().unwrap())),
+                    "usize" => Some(ParsedKey::Usize(s.base10_parse::<usize>().unwrap())),
                     _ => None,
                 },
                 Lit::Bool(s) => Some(ParsedKey::Bool(s.value)),
@@ -119,6 +125,7 @@ impl ParsedKey {
                         ParsedKey::I32(v) => Some(ParsedKey::I32(try_negate!(v))),
                         ParsedKey::I64(v) => Some(ParsedKey::I64(try_negate!(v))),
                         ParsedKey::I128(v) => Some(ParsedKey::I128(try_negate!(v))),
+                        ParsedKey::Isize(v) => Some(ParsedKey::Isize(try_negate!(v))),
                         _ => None,
                     },
                     UnOp::Deref(_) => {


Test patch:
diff --git a/phf_macros_tests/tests/test.rs b/phf_macros_tests/tests/test.rs
index 7123753..ce031a4 100644
--- a/phf_macros_tests/tests/test.rs
+++ b/phf_macros_tests/tests/test.rs
@@ -200,6 +200,22 @@ mod map {
         );
     }
 
+    #[test]
+    fn test_isize_keys() {
+        if cfg!(target_pointer_width = "16") {
+            test_key_type!(isize, 0isize => 0, 1isize => 1, 32767isize => 2, -32768isize => 3);
+        } else if cfg!(target_pointer_width = "32") {
+            test_key_type!(isize, 0isize => 0, 1isize => 1, 2147483647isize => 2, -2147483648isize => 3);
+        } else if cfg!(target_pointer_width = "64") {
+            test_key_type!(
+                isize, 0isize => 0, 1isize => 1,
+                9223372036854775807isize => 2, -9223372036854775808isize => 3
+            );
+        } else {
+            panic!("target_pointer_width is not 16, 32, or 64")
+        }
+    }
+
     #[test]
     fn test_u8_keys() {
         test_key_type!(u8, 0u8 => 0, 1u8 => 1, 255u8 => 2);
@@ -228,6 +244,19 @@ mod map {
         );
     }
 
+    #[test]
+    fn test_usize_keys() {
+        if cfg!(target_pointer_width = "16") {
+            test_key_type!(usize, 0usize => 0, 1usize => 1, 65535usize => 2);
+        } else if cfg!(target_pointer_width = "32") {
+            test_key_type!(usize, 0usize => 0, 1usize => 1, 4294967295usize => 2);
+        } else if cfg!(target_pointer_width = "64") {
+            test_key_type!(usize, 0usize => 0, 1usize => 1, 18446744073709551615usize => 2);
+        } else {
+            panic!("target_pointer_width is not 16, 32, or 64")
+        }
+    }
+
     #[test]
     fn test_bool_keys() {
         test_key_type!(bool, false => 0, true => 1);