From f0159f656d95d19b681e63b827538f6d0ca3367b Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 21 Oct 2021 14:06:55 -0700 Subject: [PATCH 1/2] Add `IndexMap::from(array)` and `IndexSet::from(array)` This patch adds `IndexMap::from(array)` and `IndexSet::from(array)` to match the API for `HashMap`, `HashSet`, etc. as of https://github.com/rust-lang/rust/pull/84111. --- src/map.rs | 29 +++++++++++++++++++++++++++++ src/set.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/map.rs b/src/map.rs index 206e7eef..ad76a473 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1345,6 +1345,25 @@ where } } +#[cfg(has_std)] +impl From<[(K, V); N]> for IndexMap +where + K: Hash + Eq, +{ + /// # Examples + /// + /// ``` + /// use indexmap::IndexMap; + /// + /// let map1 = IndexMap::from([(1, 2), (3, 4)]); + /// let map2: IndexMap<_, _> = [(1, 2), (3, 4)].into(); + /// assert_eq!(map1, map2); + /// ``` + fn from(arr: [(K, V); N]) -> Self { + std::array::IntoIter::new(arr).collect() + } +} + impl Extend<(K, V)> for IndexMap where K: Hash + Eq, @@ -1839,4 +1858,14 @@ mod tests { assert!(values.contains(&'b')); assert!(values.contains(&'c')); } + + #[test] + fn from_array() { + let map = IndexMap::from([(1, 2), (3, 4)]); + let mut expected = IndexMap::new(); + expected.insert(1, 2); + expected.insert(3, 4); + + assert_eq!(map, expected) + } } diff --git a/src/set.rs b/src/set.rs index 5aabcf51..0322d25d 100644 --- a/src/set.rs +++ b/src/set.rs @@ -843,6 +843,25 @@ where } } +#[cfg(has_std)] +impl From<[T; N]> for IndexSet +where + T: Eq + Hash, +{ + /// # Examples + /// + /// ``` + /// use indexmap::IndexSet; + /// + /// let set1 = IndexSet::from([1, 2, 3, 4]); + /// let set2: IndexSet<_> = [1, 2, 3, 4].into(); + /// assert_eq!(set1, set2); + /// ``` + fn from(arr: [T; N]) -> Self { + std::array::IntoIter::new(arr).collect() + } +} + impl Extend for IndexSet where T: Hash + Eq, @@ -1710,4 +1729,12 @@ mod tests { assert_eq!(&set_c - &set_d, set_a); assert_eq!(&set_d - &set_c, &set_d - &set_b); } + + #[test] + fn from_array() { + let set1 = IndexSet::from([1, 2, 3, 4]); + let set2: IndexSet<_> = [1, 2, 3, 4].into(); + + assert_eq!(set1, set2); + } } From 5d2ce528b3c431722581526b175a51528ae0efa0 Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Sun, 24 Oct 2021 13:15:04 -0700 Subject: [PATCH 2/2] Require rustc 1.51+ for `IndexMap::from(array)` and `IndexSet::from(array)` https://github.com/rust-lang/rust/issues/74878 and https://github.com/rust-lang/rust/issues/65798 were both stabilized in 1.51. --- build.rs | 1 + src/map.rs | 3 ++- src/set.rs | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 9f9fa054..7c5b6d5e 100644 --- a/build.rs +++ b/build.rs @@ -4,5 +4,6 @@ fn main() { Some(_) => autocfg::emit("has_std"), None => autocfg::new().emit_sysroot_crate("std"), } + autocfg::new().emit_rustc_version(1, 51); autocfg::rerun_path("build.rs"); } diff --git a/src/map.rs b/src/map.rs index ad76a473..ca51b545 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1345,7 +1345,7 @@ where } } -#[cfg(has_std)] +#[cfg(all(has_std, rustc_1_51))] impl From<[(K, V); N]> for IndexMap where K: Hash + Eq, @@ -1860,6 +1860,7 @@ mod tests { } #[test] + #[cfg(all(has_std, rustc_1_51))] fn from_array() { let map = IndexMap::from([(1, 2), (3, 4)]); let mut expected = IndexMap::new(); diff --git a/src/set.rs b/src/set.rs index 0322d25d..f7d4b431 100644 --- a/src/set.rs +++ b/src/set.rs @@ -843,7 +843,7 @@ where } } -#[cfg(has_std)] +#[cfg(all(has_std, rustc_1_51))] impl From<[T; N]> for IndexSet where T: Eq + Hash, @@ -1731,6 +1731,7 @@ mod tests { } #[test] + #[cfg(all(has_std, rustc_1_51))] fn from_array() { let set1 = IndexSet::from([1, 2, 3, 4]); let set2: IndexSet<_> = [1, 2, 3, 4].into();