From 05c7330ca03650bbcb6a55f5fa490b3bb03c1940 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sat, 30 Jul 2022 01:48:16 +0200 Subject: Implement Default for some alloc/core iterators This way one can `mem::take()` them out of structs or #[derive(Default)] on structs containing them. These changes will be insta-stable. --- library/alloc/src/collections/binary_heap/mod.rs | 7 +++ library/alloc/src/collections/btree/map.rs | 56 ++++++++++++++++++++++ library/alloc/src/collections/btree/map/tests.rs | 16 +++++++ library/alloc/src/collections/btree/navigate.rs | 12 +++++ library/alloc/src/collections/btree/set.rs | 22 +++++++++ library/alloc/src/collections/linked_list.rs | 21 ++++++++ library/alloc/src/collections/linked_list/tests.rs | 6 +++ library/alloc/src/vec/into_iter.rs | 7 +++ library/alloc/tests/vec.rs | 10 +++- 9 files changed, 156 insertions(+), 1 deletion(-) (limited to 'library/alloc') diff --git a/library/alloc/src/collections/binary_heap/mod.rs b/library/alloc/src/collections/binary_heap/mod.rs index f1d0a305d99..b0ec70ad565 100644 --- a/library/alloc/src/collections/binary_heap/mod.rs +++ b/library/alloc/src/collections/binary_heap/mod.rs @@ -1468,6 +1468,13 @@ impl ExactSizeIterator for IntoIter { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for IntoIter {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoIter { + fn default() -> Self { + IntoIter { iter: Default::default() } + } +} + // In addition to the SAFETY invariants of the following three unsafe traits // also refer to the vec::in_place_collect module documentation to get an overview #[unstable(issue = "none", feature = "inplace_iteration")] diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 386cd1a1657..c1e8a84969a 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -362,6 +362,13 @@ impl fmt::Debug for Iter<'_, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> { + fn default() -> Self { + Iter { range: Default::default(), length: 0 } + } +} + /// A mutable iterator over the entries of a `BTreeMap`. /// /// This `struct` is created by the [`iter_mut`] method on [`BTreeMap`]. See its @@ -386,6 +393,13 @@ impl fmt::Debug for IterMut<'_, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> { + fn default() -> Self { + IterMut { range: Default::default(), length: 0, _marker: PhantomData {} } + } +} + /// An owning iterator over the entries of a `BTreeMap`. /// /// This `struct` is created by the [`into_iter`] method on [`BTreeMap`] @@ -421,6 +435,13 @@ impl Debug for IntoIter { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoIter { + fn default() -> Self { + IntoIter { range: Default::default(), length: 0, alloc: Global } + } +} + /// An iterator over the keys of a `BTreeMap`. /// /// This `struct` is created by the [`keys`] method on [`BTreeMap`]. See its @@ -1768,6 +1789,13 @@ impl Clone for Keys<'_, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Keys<'_, K, V> { + fn default() -> Self { + Keys { inner: Default::default() } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, K, V> Iterator for Values<'a, K, V> { type Item = &'a V; @@ -1809,6 +1837,13 @@ impl Clone for Values<'_, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Values<'_, K, V> { + fn default() -> Self { + Values { inner: Default::default() } + } +} + /// An iterator produced by calling `drain_filter` on BTreeMap. #[unstable(feature = "btree_drain_filter", issue = "70530")] pub struct DrainFilter< @@ -1945,6 +1980,13 @@ impl<'a, K, V> Iterator for Range<'a, K, V> { } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Range<'_, K, V> { + fn default() -> Self { + Range { inner: Default::default() } + } +} + #[stable(feature = "map_values_mut", since = "1.10.0")] impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { type Item = &'a mut V; @@ -2021,6 +2063,13 @@ impl ExactSizeIterator for IntoKeys { #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl FusedIterator for IntoKeys {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoKeys { + fn default() -> Self { + IntoKeys { inner: Default::default() } + } +} + #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl Iterator for IntoValues { type Item = V; @@ -2055,6 +2104,13 @@ impl ExactSizeIterator for IntoValues { #[stable(feature = "map_into_keys_values", since = "1.54.0")] impl FusedIterator for IntoValues {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoValues { + fn default() -> Self { + IntoValues { inner: Default::default() } + } +} + #[stable(feature = "btree_range", since = "1.17.0")] impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> { fn next_back(&mut self) -> Option<(&'a K, &'a V)> { diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index 76c2f27b466..4d011195936 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -563,6 +563,22 @@ fn test_iter_min_max() { a.check(); } +#[test] +fn test_iters_default() { + let iter: Keys<'_, u8, u8> = Keys::default(); + assert_eq!(iter.len(), 0); + let iter: Values<'_, u8, u8> = Values::default(); + assert_eq!(iter.len(), 0); + let iter: Range<'_, u8, u8> = Range::default(); + assert_eq!(iter.count(), 0); + let iter: IntoIter = IntoIter::default(); + assert_eq!(iter.len(), 0); + let iter: IntoKeys = IntoKeys::default(); + assert_eq!(iter.len(), 0); + let iter: IntoValues = IntoValues::default(); + assert_eq!(iter.len(), 0); +} + fn range_keys(map: &BTreeMap, range: impl RangeBounds) -> Vec { Vec::from_iter(map.range(range).map(|(&k, &v)| { assert_eq!(k, v); diff --git a/library/alloc/src/collections/btree/navigate.rs b/library/alloc/src/collections/btree/navigate.rs index b890717e50b..a85a3162451 100644 --- a/library/alloc/src/collections/btree/navigate.rs +++ b/library/alloc/src/collections/btree/navigate.rs @@ -19,6 +19,12 @@ impl<'a, K: 'a, V: 'a> Clone for LeafRange, K, V> { } } +impl Default for LeafRange { + fn default() -> Self { + LeafRange { front: None, back: None } + } +} + impl LeafRange { pub fn none() -> Self { LeafRange { front: None, back: None } @@ -124,6 +130,12 @@ pub struct LazyLeafRange { back: Option>, } +impl Default for LazyLeafRange { + fn default() -> Self { + LazyLeafRange { front: None, back: None } + } +} + impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange, K, V> { fn clone(&self) -> Self { LazyLeafRange { front: self.front.clone(), back: self.back.clone() } diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 4ddb2119252..897499db429 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -1544,6 +1544,14 @@ impl Iterator for IntoIter { self.iter.size_hint() } } + +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Iter<'_, T> { + fn default() -> Self { + Iter { iter: Default::default() } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option { @@ -1560,6 +1568,13 @@ impl ExactSizeIterator for IntoIter { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for IntoIter {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoIter { + fn default() -> Self { + IntoIter { iter: Default::default() } + } +} + #[stable(feature = "btree_range", since = "1.17.0")] impl Clone for Range<'_, T> { fn clone(&self) -> Self { @@ -1598,6 +1613,13 @@ impl<'a, T> DoubleEndedIterator for Range<'a, T> { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Range<'_, T> {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Range<'_, T> { + fn default() -> Self { + Range { iter: Default::default() } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Clone for Difference<'_, T, A> { fn clone(&self) -> Self { diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index f2f5dffc25d..dae7fc48b1f 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -1075,6 +1075,13 @@ impl ExactSizeIterator for Iter<'_, T> {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Iter<'_, T> {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Iter<'_, T> { + fn default() -> Self { + Iter { head: None, tail: None, len: 0, marker: Default::default() } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; @@ -1129,6 +1136,13 @@ impl ExactSizeIterator for IterMut<'_, T> {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for IterMut<'_, T> {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IterMut<'_, T> { + fn default() -> Self { + IterMut { head: None, tail: None, len: 0, marker: Default::default() } + } +} + /// A cursor over a `LinkedList`. /// /// A `Cursor` is like an iterator, except that it can freely seek back-and-forth. @@ -1808,6 +1822,13 @@ impl ExactSizeIterator for IntoIter {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for IntoIter {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoIter { + fn default() -> Self { + LinkedList::new().into_iter() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl FromIterator for LinkedList { fn from_iter>(iter: I) -> Self { diff --git a/library/alloc/src/collections/linked_list/tests.rs b/library/alloc/src/collections/linked_list/tests.rs index 04594d55b6a..075c68f7241 100644 --- a/library/alloc/src/collections/linked_list/tests.rs +++ b/library/alloc/src/collections/linked_list/tests.rs @@ -172,6 +172,12 @@ fn test_iterator() { assert_eq!(it.next(), None); } +#[test] +fn test_default() { + let iter: IntoIter = Default::default(); + assert_eq!(iter.len(), 0); +} + #[test] fn test_iterator_clone() { let mut n = LinkedList::new(); diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 37966007eb7..2be484c3dd4 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -347,6 +347,13 @@ impl FusedIterator for IntoIter {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for IntoIter {} +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for IntoIter { + fn default() -> Self { + super::Vec::new().into_iter() + } +} + #[doc(hidden)] #[unstable(issue = "none", feature = "std_internals")] #[rustc_unsafe_specialization_marker] diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 2f07c2911a5..782b150681c 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1,5 +1,6 @@ use core::alloc::{Allocator, Layout}; -use core::iter::IntoIterator; +use core::assert_eq; +use core::iter::{ExactSizeIterator, IntoIterator}; use core::ptr::NonNull; use std::alloc::System; use std::assert_matches::assert_matches; @@ -1035,6 +1036,13 @@ fn test_into_iter_clone() { assert_eq!(it.next(), None); } +#[test] +fn test_into_iter_default() { + let iter: IntoIter = Default::default(); + assert_eq!(iter.len(), 0); + assert_eq!(iter.as_slice(), &[]); +} + #[test] fn test_into_iter_leak() { static mut DROPS: i32 = 0; -- cgit 1.4.1-3-g733a5