Skip to content

Commit db9016d

Browse files
Implement Default for iterators
This matches Default implementations introduced for HashMap and HashSet in Rust 1.70.
1 parent 11ac52c commit db9016d

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/map/iter.rs

+58
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
8989
}
9090
}
9191

92+
impl<K, V> Default for Iter<'_, K, V> {
93+
fn default() -> Self {
94+
Self { iter: [].iter() }
95+
}
96+
}
97+
9298
/// A mutable iterator over the entries of a `IndexMap`.
9399
///
94100
/// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its
@@ -145,6 +151,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
145151
}
146152
}
147153

154+
impl<K, V> Default for IterMut<'_, K, V> {
155+
fn default() -> Self {
156+
Self {
157+
iter: [].iter_mut(),
158+
}
159+
}
160+
}
161+
148162
/// An owning iterator over the entries of a `IndexMap`.
149163
///
150164
/// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
@@ -199,6 +213,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
199213
}
200214
}
201215

216+
impl<K, V> Default for IntoIter<K, V> {
217+
fn default() -> Self {
218+
Self {
219+
iter: Vec::new().into_iter(),
220+
}
221+
}
222+
}
223+
202224
/// A draining iterator over the entries of a `IndexMap`.
203225
///
204226
/// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its
@@ -298,6 +320,12 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
298320
}
299321
}
300322

323+
impl<K, V> Default for Keys<'_, K, V> {
324+
fn default() -> Self {
325+
Self { iter: [].iter() }
326+
}
327+
}
328+
301329
/// An owning iterator over the keys of a `IndexMap`.
302330
///
303331
/// This `struct` is created by the [`into_keys`] method on [`IndexMap`].
@@ -342,6 +370,14 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
342370
}
343371
}
344372

373+
impl<K, V> Default for IntoKeys<K, V> {
374+
fn default() -> Self {
375+
Self {
376+
iter: Vec::new().into_iter(),
377+
}
378+
}
379+
}
380+
345381
/// An iterator over the values of a `IndexMap`.
346382
///
347383
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
@@ -394,6 +430,12 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
394430
}
395431
}
396432

433+
impl<K, V> Default for Values<'_, K, V> {
434+
fn default() -> Self {
435+
Self { iter: [].iter() }
436+
}
437+
}
438+
397439
/// A mutable iterator over the values of a `IndexMap`.
398440
///
399441
/// This `struct` is created by the [`values_mut`] method on [`IndexMap`]. See its
@@ -438,6 +480,14 @@ impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
438480
}
439481
}
440482

483+
impl<K, V> Default for ValuesMut<'_, K, V> {
484+
fn default() -> Self {
485+
Self {
486+
iter: [].iter_mut(),
487+
}
488+
}
489+
}
490+
441491
/// An owning iterator over the values of a `IndexMap`.
442492
///
443493
/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
@@ -481,3 +531,11 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
481531
f.debug_list().entries(iter).finish()
482532
}
483533
}
534+
535+
impl<K, V> Default for IntoValues<K, V> {
536+
fn default() -> Self {
537+
Self {
538+
iter: Vec::new().into_iter(),
539+
}
540+
}
541+
}

src/map/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,20 @@ fn from_array() {
427427

428428
assert_eq!(map, expected)
429429
}
430+
431+
#[test]
432+
fn iter_default() {
433+
struct K;
434+
struct V;
435+
fn assert_default<T: Default + Iterator>() {
436+
assert!(T::default().next().is_none());
437+
}
438+
assert_default::<Iter<'static, K, V>>();
439+
assert_default::<IterMut<'static, K, V>>();
440+
assert_default::<IntoIter<K, V>>();
441+
assert_default::<Keys<'static, K, V>>();
442+
assert_default::<IntoKeys<K, V>>();
443+
assert_default::<Values<'static, K, V>>();
444+
assert_default::<ValuesMut<'static, K, V>>();
445+
assert_default::<IntoValues<K, V>>();
446+
}

src/set/iter.rs

+14
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
8080
}
8181
}
8282

83+
impl<T> Default for Iter<'_, T> {
84+
fn default() -> Self {
85+
Self { iter: [].iter() }
86+
}
87+
}
88+
8389
/// An owning iterator over the items of a `IndexSet`.
8490
///
8591
/// This `struct` is created by the [`into_iter`] method on [`IndexSet`]
@@ -129,6 +135,14 @@ impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
129135
}
130136
}
131137

138+
impl<T> Default for IntoIter<T> {
139+
fn default() -> Self {
140+
Self {
141+
iter: Vec::new().into_iter(),
142+
}
143+
}
144+
}
145+
132146
/// A draining iterator over the items of a `IndexSet`.
133147
///
134148
/// This `struct` is created by the [`drain`] method on [`IndexSet`].

src/set/tests.rs

+13
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,16 @@ fn from_array() {
530530

531531
assert_eq!(set1, set2);
532532
}
533+
534+
#[test]
535+
fn iter_default() {
536+
struct Item;
537+
fn assert_default<T>()
538+
where
539+
T: Default + Iterator,
540+
{
541+
assert!(T::default().next().is_none());
542+
}
543+
assert_default::<Iter<'static, Item>>();
544+
assert_default::<IntoIter<Item>>();
545+
}

0 commit comments

Comments
 (0)