Skip to content

Commit

Permalink
Implement Default for iterators
Browse files Browse the repository at this point in the history
This matches Default implementations introduced for HashMap and
HashSet in Rust 1.70.
  • Loading branch information
KamilaBorowska committed May 30, 2023
1 parent 11ac52c commit db9016d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/map/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
}
}

impl<K, V> 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
Expand Down Expand Up @@ -145,6 +151,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
}
}

impl<K, V> 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`]
Expand Down Expand Up @@ -199,6 +213,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
}
}

impl<K, V> Default for IntoIter<K, V> {
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
Expand Down Expand Up @@ -298,6 +320,12 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
}
}

impl<K, V> 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`].
Expand Down Expand Up @@ -342,6 +370,14 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
}
}

impl<K, V> Default for IntoKeys<K, V> {
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
Expand Down Expand Up @@ -394,6 +430,12 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
}
}

impl<K, V> 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
Expand Down Expand Up @@ -438,6 +480,14 @@ impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
}
}

impl<K, V> 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`].
Expand Down Expand Up @@ -481,3 +531,11 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
f.debug_list().entries(iter).finish()
}
}

impl<K, V> Default for IntoValues<K, V> {
fn default() -> Self {
Self {
iter: Vec::new().into_iter(),
}
}
}
17 changes: 17 additions & 0 deletions src/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,20 @@ fn from_array() {

assert_eq!(map, expected)
}

#[test]
fn iter_default() {
struct K;
struct V;
fn assert_default<T: Default + Iterator>() {
assert!(T::default().next().is_none());
}
assert_default::<Iter<'static, K, V>>();
assert_default::<IterMut<'static, K, V>>();
assert_default::<IntoIter<K, V>>();
assert_default::<Keys<'static, K, V>>();
assert_default::<IntoKeys<K, V>>();
assert_default::<Values<'static, K, V>>();
assert_default::<ValuesMut<'static, K, V>>();
assert_default::<IntoValues<K, V>>();
}
14 changes: 14 additions & 0 deletions src/set/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
}
}

impl<T> 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`]
Expand Down Expand Up @@ -129,6 +135,14 @@ impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
}
}

impl<T> Default for IntoIter<T> {
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`].
Expand Down
13 changes: 13 additions & 0 deletions src/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,16 @@ fn from_array() {

assert_eq!(set1, set2);
}

#[test]
fn iter_default() {
struct Item;
fn assert_default<T>()
where
T: Default + Iterator,
{
assert!(T::default().next().is_none());
}
assert_default::<Iter<'static, Item>>();
assert_default::<IntoIter<Item>>();
}

0 comments on commit db9016d

Please sign in to comment.