From 704bf5ade8880635c7e5fca16f34a632fb00e825 Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Tue, 30 May 2023 18:39:34 +0200 Subject: [PATCH] Implement Default for iterators This matches Default implementations for HashMap and HashSet introduced in Rust 1.70. --- src/map/iter.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ src/map/tests.rs | 20 +++++++++++++++++ src/set/iter.rs | 14 ++++++++++++ src/set/tests.rs | 13 +++++++++++ 4 files changed, 105 insertions(+) diff --git a/src/map/iter.rs b/src/map/iter.rs index 6cd933e9..db6e140d 100644 --- a/src/map/iter.rs +++ b/src/map/iter.rs @@ -89,6 +89,12 @@ impl fmt::Debug for Iter<'_, K, V> { } } +impl Default for Iter<'_, K, V> { + fn default() -> Self { + Self { iter: [].iter() } + } +} + /// A mutable iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its @@ -145,6 +151,14 @@ impl fmt::Debug for IterMut<'_, K, V> { } } +impl Default for IterMut<'_, K, V> { + fn default() -> Self { + Self { + iter: [].iter_mut(), + } + } +} + /// An owning iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`into_iter`] method on [`IndexMap`] @@ -199,6 +213,14 @@ impl fmt::Debug for IntoIter { } } +impl Default for IntoIter { + fn default() -> Self { + Self { + iter: Vec::new().into_iter(), + } + } +} + /// A draining iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its @@ -298,6 +320,12 @@ impl fmt::Debug for Keys<'_, K, V> { } } +impl Default for Keys<'_, K, V> { + fn default() -> Self { + Self { iter: [].iter() } + } +} + /// An owning iterator over the keys of a `IndexMap`. /// /// This `struct` is created by the [`into_keys`] method on [`IndexMap`]. @@ -342,6 +370,14 @@ impl fmt::Debug for IntoKeys { } } +impl Default for IntoKeys { + fn default() -> Self { + Self { + iter: Vec::new().into_iter(), + } + } +} + /// An iterator over the values of a `IndexMap`. /// /// This `struct` is created by the [`values`] method on [`IndexMap`]. See its @@ -394,6 +430,12 @@ impl fmt::Debug for Values<'_, K, V> { } } +impl Default for Values<'_, K, V> { + fn default() -> Self { + Self { iter: [].iter() } + } +} + /// A mutable iterator over the values of a `IndexMap`. /// /// This `struct` is created by the [`values_mut`] method on [`IndexMap`]. See its @@ -438,6 +480,14 @@ impl fmt::Debug for ValuesMut<'_, K, V> { } } +impl Default for ValuesMut<'_, K, V> { + fn default() -> Self { + Self { + iter: [].iter_mut(), + } + } +} + /// An owning iterator over the values of a `IndexMap`. /// /// This `struct` is created by the [`into_values`] method on [`IndexMap`]. @@ -481,3 +531,11 @@ impl fmt::Debug for IntoValues { f.debug_list().entries(iter).finish() } } + +impl Default for IntoValues { + fn default() -> Self { + Self { + iter: Vec::new().into_iter(), + } + } +} diff --git a/src/map/tests.rs b/src/map/tests.rs index 423e3f3d..f273d716 100644 --- a/src/map/tests.rs +++ b/src/map/tests.rs @@ -427,3 +427,23 @@ fn from_array() { assert_eq!(map, expected) } + +#[test] +fn iter_default() { + struct K; + struct V; + fn assert_default() + where + T: Default + Iterator, + { + assert!(T::default().next().is_none()); + } + assert_default::>(); + assert_default::>(); + assert_default::>(); + assert_default::>(); + assert_default::>(); + assert_default::>(); + assert_default::>(); + assert_default::>(); +} diff --git a/src/set/iter.rs b/src/set/iter.rs index 24b3e86c..828756da 100644 --- a/src/set/iter.rs +++ b/src/set/iter.rs @@ -80,6 +80,12 @@ impl fmt::Debug for Iter<'_, T> { } } +impl Default for Iter<'_, T> { + fn default() -> Self { + Self { iter: [].iter() } + } +} + /// An owning iterator over the items of a `IndexSet`. /// /// This `struct` is created by the [`into_iter`] method on [`IndexSet`] @@ -129,6 +135,14 @@ impl fmt::Debug for IntoIter { } } +impl Default for IntoIter { + fn default() -> Self { + Self { + iter: Vec::new().into_iter(), + } + } +} + /// A draining iterator over the items of a `IndexSet`. /// /// This `struct` is created by the [`drain`] method on [`IndexSet`]. diff --git a/src/set/tests.rs b/src/set/tests.rs index 39e110f4..44f8ed84 100644 --- a/src/set/tests.rs +++ b/src/set/tests.rs @@ -530,3 +530,16 @@ fn from_array() { assert_eq!(set1, set2); } + +#[test] +fn iter_default() { + struct Item; + fn assert_default() + where + T: Default + Iterator, + { + assert!(T::default().next().is_none()); + } + assert_default::>(); + assert_default::>(); +}