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

Issue:
Implement `FromIterator` for `phf_codegen::Map`
Enables [`Iterator::collect()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect).

Pull request description:
feat(codegen): Implement `FromIterator` for `Map`
Fixes #307


Patch:
diff --git a/.github/workflows/benches.yml b/.github/workflows/benches.yml
index cb5833c..8a41997 100644
--- a/.github/workflows/benches.yml
+++ b/.github/workflows/benches.yml
@@ -8,7 +8,7 @@ permissions:
   contents: write
 
 jobs:
-  bench_between_master_and_pr:
+  bench_between_main_and_pr:
     if: ${{ github.event.label.name == 'run-benchmarks' }}
     name: Bench
     runs-on: ubuntu-latest
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 9f465be..2cb85a4 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -2,7 +2,7 @@ name: CI
 
 on:
   push:
-    branches: [master]
+    branches: [main]
   pull_request:
 
 jobs:
diff --git a/phf_codegen/src/lib.rs b/phf_codegen/src/lib.rs
index 49133ec..24fa095 100644
--- a/phf_codegen/src/lib.rs
+++ b/phf_codegen/src/lib.rs
@@ -280,6 +280,20 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
     }
 }
 
+impl<'a, K, V> FromIterator<(K, V)> for Map<'a, K>
+where
+    K: Hash + PhfHash + Eq + FmtConst,
+    V: Into<Cow<'a, str>>,
+{
+    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
+        let mut map = Map::new();
+        for (key, value) in iter {
+            map.entry(key, value);
+        }
+        map
+    }
+}
+
 /// A builder for the `phf::Set` type.
 pub struct Set<'a, T> {
     map: Map<'a, T>,


Test patch:
diff --git a/phf_codegen/test/build.rs b/phf_codegen/test/build.rs
index 031c7d1..6cd3585 100644
--- a/phf_codegen/test/build.rs
+++ b/phf_codegen/test/build.rs
@@ -120,5 +120,15 @@ fn main() -> io::Result<()> {
             .entry(b"baz", "2")
             .entry(b"quux", "3")
             .build()
+    )?;
+
+    // Test FromIterator implementation
+    writeln!(
+        &mut file,
+        "static FROM_ITER_MAP: ::phf::Map<&'static str, u32> = \n{};",
+        vec![("one", "1"), ("two", "2"), ("three", "3")]
+            .into_iter()
+            .collect::<phf_codegen::Map<_>>()
+            .build()
     )
 }
diff --git a/phf_codegen/test/src/lib.rs b/phf_codegen/test/src/lib.rs
index a82f535..9778307 100644
--- a/phf_codegen/test/src/lib.rs
+++ b/phf_codegen/test/src/lib.rs
@@ -117,4 +117,12 @@ mod test {
     fn empty_ordered_map() {
         assert_eq!(None, EMPTY_ORDERED.get(&1));
     }
+
+    #[test]
+    fn from_iter_map() {
+        assert_eq!(1, FROM_ITER_MAP["one"]);
+        assert_eq!(2, FROM_ITER_MAP["two"]);
+        assert_eq!(3, FROM_ITER_MAP["three"]);
+        assert!(!FROM_ITER_MAP.contains_key("four"));
+    }
 }