From 50835bf1943193f7a78cc39b17f6e90b72bff5a4 Mon Sep 17 00:00:00 2001 From: ltdk Date: Sat, 27 Jul 2024 01:51:04 -0400 Subject: [PATCH 1/2] impl Default for collection iterators that don't already have it --- .../alloc/src/collections/binary_heap/mod.rs | 14 ++++++++++ library/alloc/src/collections/btree/map.rs | 28 +++++++++++++++++++ .../alloc/src/collections/vec_deque/iter.rs | 14 ++++++++++ .../src/collections/vec_deque/iter_mut.rs | 14 ++++++++++ 4 files changed, 70 insertions(+) diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index fe1ff24139554..a4a172b86e54a 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1433,6 +1433,20 @@ pub struct Iter<'a, T: 'a> { iter: slice::Iter<'a, T>, } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Iter<'_, T> { + /// Creates an empty `binary_heap::Iter`. + /// + /// ``` + /// # use std::collections::binary_heap; + /// let iter: binary_heap::Iter<'_, u8> = Default::default(); + /// assert_eq!(iter.len(), 0); + /// ``` + fn default() -> Self { + Iter { iter: Default::default() } + } +} + #[stable(feature = "collection_debug", since = "1.17.0")] impl fmt::Debug for Iter<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 3875f61efafdf..a2e97bcee829e 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2016,6 +2016,20 @@ impl Default for Range<'_, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for RangeMut<'_, K, V> { + /// Creates an empty `btree_map::RangeMut`. + /// + /// ``` + /// # use std::collections::btree_map; + /// let iter: btree_map::RangeMut<'_, u8, u8> = Default::default(); + /// assert_eq!(iter.count(), 0); + /// ``` + fn default() -> Self { + RangeMut { inner: Default::default(), _marker: PhantomData } + } +} + #[stable(feature = "map_values_mut", since = "1.10.0")] impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { type Item = &'a mut V; @@ -2050,6 +2064,20 @@ impl ExactSizeIterator for ValuesMut<'_, K, V> { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for ValuesMut<'_, K, V> {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for ValuesMut<'_, K, V> { + /// Creates an empty `btree_map::ValuesMut`. + /// + /// ``` + /// # use std::collections::btree_map; + /// let iter: btree_map::ValuesMut<'_, u8, u8> = Default::default(); + /// assert_eq!(iter.count(), 0); + /// ``` + fn default() -> Self { + ValuesMut { inner: Default::default() } + } +} + #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl Iterator for IntoKeys { type Item = K; diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index 5a5e7f70854d8..10adc547da666 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -28,6 +28,20 @@ impl fmt::Debug for Iter<'_, T> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Iter<'_, T> { + /// Creates an empty `vec_deque::Iter`. + /// + /// ``` + /// # use std::collections::vec_deque; + /// let iter: vec_deque::Iter<'_, u8> = Default::default(); + /// assert_eq!(iter.len(), 0); + /// ``` + fn default() -> Self { + Iter { i1: Default::default(), i2: Default::default() } + } +} + // FIXME(#26925) Remove in favor of `#[derive(Clone)]` #[stable(feature = "rust1", since = "1.0.0")] impl Clone for Iter<'_, T> { diff --git a/library/alloc/src/collections/vec_deque/iter_mut.rs b/library/alloc/src/collections/vec_deque/iter_mut.rs index 5061931afb7b7..18f11096eeb93 100644 --- a/library/alloc/src/collections/vec_deque/iter_mut.rs +++ b/library/alloc/src/collections/vec_deque/iter_mut.rs @@ -28,6 +28,20 @@ impl fmt::Debug for IterMut<'_, T> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IterMut<'_, T> { + /// Creates an empty `vec_deque::IterMut`. + /// + /// ``` + /// # use std::collections::vec_deque; + /// let iter: vec_deque::IterMut<'_, u8> = Default::default(); + /// assert_eq!(iter.len(), 0); + /// ``` + fn default() -> Self { + IterMut { i1: Default::default(), i2: Default::default() } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; From 0b9972049727f08610c038bce7177571b7111df5 Mon Sep 17 00:00:00 2001 From: ltdk Date: Sat, 27 Jul 2024 02:48:55 -0400 Subject: [PATCH 2/2] Okay, I guess I have to give these a different feature name --- library/alloc/src/collections/binary_heap/mod.rs | 2 +- library/alloc/src/collections/btree/map.rs | 4 ++-- library/alloc/src/collections/vec_deque/iter.rs | 2 +- library/alloc/src/collections/vec_deque/iter_mut.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index a4a172b86e54a..72e466f4dcdc4 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1433,7 +1433,7 @@ pub struct Iter<'a, T: 'a> { iter: slice::Iter<'a, T>, } -#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")] impl Default for Iter<'_, T> { /// Creates an empty `binary_heap::Iter`. /// diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index a2e97bcee829e..2c28e893f3696 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2016,7 +2016,7 @@ impl Default for Range<'_, K, V> { } } -#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")] impl Default for RangeMut<'_, K, V> { /// Creates an empty `btree_map::RangeMut`. /// @@ -2064,7 +2064,7 @@ impl ExactSizeIterator for ValuesMut<'_, K, V> { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for ValuesMut<'_, K, V> {} -#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")] impl Default for ValuesMut<'_, K, V> { /// Creates an empty `btree_map::ValuesMut`. /// diff --git a/library/alloc/src/collections/vec_deque/iter.rs b/library/alloc/src/collections/vec_deque/iter.rs index 10adc547da666..67b5b91c4d4b0 100644 --- a/library/alloc/src/collections/vec_deque/iter.rs +++ b/library/alloc/src/collections/vec_deque/iter.rs @@ -28,7 +28,7 @@ impl fmt::Debug for Iter<'_, T> { } } -#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")] impl Default for Iter<'_, T> { /// Creates an empty `vec_deque::Iter`. /// diff --git a/library/alloc/src/collections/vec_deque/iter_mut.rs b/library/alloc/src/collections/vec_deque/iter_mut.rs index 18f11096eeb93..2726e3e425290 100644 --- a/library/alloc/src/collections/vec_deque/iter_mut.rs +++ b/library/alloc/src/collections/vec_deque/iter_mut.rs @@ -28,7 +28,7 @@ impl fmt::Debug for IterMut<'_, T> { } } -#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")] impl Default for IterMut<'_, T> { /// Creates an empty `vec_deque::IterMut`. ///