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 206e7eef..ca51b545 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1345,6 +1345,25 @@ where } } +#[cfg(all(has_std, rustc_1_51))] +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,15 @@ mod tests { assert!(values.contains(&'b')); assert!(values.contains(&'c')); } + + #[test] + #[cfg(all(has_std, rustc_1_51))] + 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..f7d4b431 100644 --- a/src/set.rs +++ b/src/set.rs @@ -843,6 +843,25 @@ where } } +#[cfg(all(has_std, rustc_1_51))] +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,13 @@ mod tests { assert_eq!(&set_c - &set_d, set_a); assert_eq!(&set_d - &set_c, &set_d - &set_b); } + + #[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(); + + assert_eq!(set1, set2); + } }