diff options
| author | bors <bors@rust-lang.org> | 2021-12-05 12:53:01 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-12-05 12:53:01 +0000 |
| commit | 1597728ef5820d3ffcb9d3f0c890ef7802398751 (patch) | |
| tree | 1a86c2a2cb56d502776e6cc3936ec683efb62ffb /library | |
| parent | cafc4582e66c478b6b297ae85b225c788106015e (diff) | |
| parent | 27d39357b7052d96e1b3903518841d14534c38cf (diff) | |
| download | rust-1597728ef5820d3ffcb9d3f0c890ef7802398751.tar.gz rust-1597728ef5820d3ffcb9d3f0c890ef7802398751.zip | |
Auto merge of #88611 - m-ou-se:array-into-iter-new-deprecate, r=joshtriplett
Deprecate array::IntoIter::new.
Diffstat (limited to 'library')
| -rw-r--r-- | library/alloc/src/collections/binary_heap.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 8 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 2 | ||||
| -rw-r--r-- | library/alloc/src/collections/linked_list.rs | 2 | ||||
| -rw-r--r-- | library/core/src/array/iter.rs | 50 | ||||
| -rw-r--r-- | library/core/src/array/mod.rs | 21 | ||||
| -rw-r--r-- | library/core/src/primitive_docs.rs | 3 | ||||
| -rw-r--r-- | library/core/tests/iter/adapters/flatten.rs | 11 | ||||
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 2 | ||||
| -rw-r--r-- | library/std/src/collections/hash/set.rs | 2 | ||||
| -rw-r--r-- | library/std/src/primitive_docs.rs | 3 |
11 files changed, 42 insertions, 64 deletions
diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 7d87974b47e..76fbfa9fc59 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -1500,7 +1500,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> { /// } /// ``` fn from(arr: [T; N]) -> Self { - core::array::IntoIter::new(arr).collect() + Self::from_iter(arr) } } diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 2ff7b0fbb75..3bd5b8ddf08 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -1305,11 +1305,11 @@ impl<K, V> BTreeMap<K, V> { pub(crate) fn bulk_build_from_sorted_iter<I>(iter: I) -> Self where K: Ord, - I: Iterator<Item = (K, V)>, + I: IntoIterator<Item = (K, V)>, { let mut root = Root::new(); let mut length = 0; - root.bulk_push(DedupSortedIter::new(iter), &mut length); + root.bulk_push(DedupSortedIter::new(iter.into_iter()), &mut length); BTreeMap { root: Some(root), length } } } @@ -1944,7 +1944,7 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> { // use stable sort to preserve the insertion order. inputs.sort_by(|a, b| a.0.cmp(&b.0)); - BTreeMap::bulk_build_from_sorted_iter(inputs.into_iter()) + BTreeMap::bulk_build_from_sorted_iter(inputs) } } @@ -2061,7 +2061,7 @@ impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> { // use stable sort to preserve the insertion order. arr.sort_by(|a, b| a.0.cmp(&b.0)); - BTreeMap::bulk_build_from_sorted_iter(core::array::IntoIter::new(arr)) + BTreeMap::bulk_build_from_sorted_iter(arr) } } diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 0322cabccde..4a83cdb917c 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -1106,7 +1106,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> { // use stable sort to preserve the insertion order. arr.sort(); - let iter = core::array::IntoIter::new(arr).map(|k| (k, ())); + let iter = IntoIterator::into_iter(arr).map(|k| (k, ())); let map = BTreeMap::bulk_build_from_sorted_iter(iter); BTreeSet { map } } diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index e4913b16adb..c8aad4877e9 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -1961,7 +1961,7 @@ impl<T, const N: usize> From<[T; N]> for LinkedList<T> { /// assert_eq!(list1, list2); /// ``` fn from(arr: [T; N]) -> Self { - core::array::IntoIter::new(arr).collect() + Self::from_iter(arr) } } diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index 5d63cf03fcb..fe7b3576e2f 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -34,30 +34,23 @@ pub struct IntoIter<T, const N: usize> { alive: Range<usize>, } -impl<T, const N: usize> IntoIter<T, N> { - /// Creates a new iterator over the given `array`. - /// - /// *Note*: this method might be deprecated in the future, - /// since [`IntoIterator`] is now implemented for arrays. - /// - /// # Examples - /// - /// ``` - /// use std::array; +// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator` +// hides this implementation from explicit `.into_iter()` calls on editions < 2021, +// so those calls will still resolve to the slice implementation, by reference. +#[stable(feature = "array_into_iter_impl", since = "1.53.0")] +impl<T, const N: usize> IntoIterator for [T; N] { + type Item = T; + type IntoIter = IntoIter<T, N>; + + /// Creates a consuming iterator, that is, one that moves each value out of + /// the array (from start to end). The array cannot be used after calling + /// this unless `T` implements `Copy`, so the whole array is copied. /// - /// for value in array::IntoIter::new([1, 2, 3, 4, 5]) { - /// // The type of `value` is an `i32` here, instead of `&i32` - /// let _: i32 = value; - /// } + /// Arrays have special behavior when calling `.into_iter()` prior to the + /// 2021 edition -- see the [array] Editions section for more information. /// - /// // Since Rust 1.53, arrays implement IntoIterator directly: - /// for value in [1, 2, 3, 4, 5] { - /// // The type of `value` is an `i32` here, instead of `&i32` - /// let _: i32 = value; - /// } - /// ``` - #[stable(feature = "array_value_iter", since = "1.51.0")] - pub fn new(array: [T; N]) -> Self { + /// [array]: prim@array + fn into_iter(self) -> Self::IntoIter { // SAFETY: The transmute here is actually safe. The docs of `MaybeUninit` // promise: // @@ -76,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> { // Until then, we can use `mem::transmute_copy` to create a bitwise copy // as a different type, then forget `array` so that it is not dropped. unsafe { - let iter = Self { data: mem::transmute_copy(&array), alive: 0..N }; - mem::forget(array); + let iter = IntoIter { data: mem::transmute_copy(&self), alive: 0..N }; + mem::forget(self); iter } } +} + +impl<T, const N: usize> IntoIter<T, N> { + /// Creates a new iterator over the given `array`. + #[stable(feature = "array_value_iter", since = "1.51.0")] + #[rustc_deprecated(since = "1.59.0", reason = "use `IntoIterator::into_iter` instead")] + pub fn new(array: [T; N]) -> Self { + IntoIterator::into_iter(array) + } /// Returns an immutable slice of all elements that have not been yielded /// yet. diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index d635829151e..23fd1453e54 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -243,27 +243,6 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] { } } -// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator` -// hides this implementation from explicit `.into_iter()` calls on editions < 2021, -// so those calls will still resolve to the slice implementation, by reference. -#[stable(feature = "array_into_iter_impl", since = "1.53.0")] -impl<T, const N: usize> IntoIterator for [T; N] { - type Item = T; - type IntoIter = IntoIter<T, N>; - - /// Creates a consuming iterator, that is, one that moves each value out of - /// the array (from start to end). The array cannot be used after calling - /// this unless `T` implements `Copy`, so the whole array is copied. - /// - /// Arrays have special behavior when calling `.into_iter()` prior to the - /// 2021 edition -- see the [array] Editions section for more information. - /// - /// [array]: prim@array - fn into_iter(self) -> Self::IntoIter { - IntoIter::new(self) - } -} - #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, const N: usize> IntoIterator for &'a [T; N] { type Item = &'a T; diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index f47a30c9b5d..8fcd8cdeb10 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -606,8 +606,7 @@ mod prim_pointer {} /// println!("array[{}] = {}", i, x); /// } /// -/// // You can explicitly iterate an array by value using -/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`: +/// // You can explicitly iterate an array by value using `IntoIterator::into_iter` /// for item in IntoIterator::into_iter(array).enumerate() { /// let (i, x): (usize, i32) = item; /// println!("array[{}] = {}", i, x); diff --git a/library/core/tests/iter/adapters/flatten.rs b/library/core/tests/iter/adapters/flatten.rs index cd6513327f0..f8ab8c9d444 100644 --- a/library/core/tests/iter/adapters/flatten.rs +++ b/library/core/tests/iter/adapters/flatten.rs @@ -1,5 +1,4 @@ use super::*; -use core::array; use core::iter::*; #[test] @@ -134,7 +133,7 @@ fn test_double_ended_flatten() { #[test] fn test_trusted_len_flatten() { fn assert_trusted_len<T: TrustedLen>(_: &T) {} - let mut iter = array::IntoIter::new([[0; 3]; 4]).flatten(); + let mut iter = IntoIterator::into_iter([[0; 3]; 4]).flatten(); assert_trusted_len(&iter); assert_eq!(iter.size_hint(), (12, Some(12))); @@ -143,21 +142,21 @@ fn test_trusted_len_flatten() { iter.next_back(); assert_eq!(iter.size_hint(), (10, Some(10))); - let iter = array::IntoIter::new([[(); usize::MAX]; 1]).flatten(); + let iter = IntoIterator::into_iter([[(); usize::MAX]; 1]).flatten(); assert_eq!(iter.size_hint(), (usize::MAX, Some(usize::MAX))); - let iter = array::IntoIter::new([[(); usize::MAX]; 2]).flatten(); + let iter = IntoIterator::into_iter([[(); usize::MAX]; 2]).flatten(); assert_eq!(iter.size_hint(), (usize::MAX, None)); let mut a = [(); 10]; let mut b = [(); 10]; - let iter = array::IntoIter::new([&mut a, &mut b]).flatten(); + let iter = IntoIterator::into_iter([&mut a, &mut b]).flatten(); assert_trusted_len(&iter); assert_eq!(iter.size_hint(), (20, Some(20))); core::mem::drop(iter); - let iter = array::IntoIter::new([&a, &b]).flatten(); + let iter = IntoIterator::into_iter([&a, &b]).flatten(); assert_trusted_len(&iter); assert_eq!(iter.size_hint(), (20, Some(20))); diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 12246b5173d..ce34e235f5d 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1186,7 +1186,7 @@ where /// assert_eq!(map1, map2); /// ``` fn from(arr: [(K, V); N]) -> Self { - crate::array::IntoIter::new(arr).collect() + Self::from_iter(arr) } } diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 1fc1d39b181..3f264ee6732 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1022,7 +1022,7 @@ where /// assert_eq!(set1, set2); /// ``` fn from(arr: [T; N]) -> Self { - crate::array::IntoIter::new(arr).collect() + Self::from_iter(arr) } } diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index f47a30c9b5d..8fcd8cdeb10 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -606,8 +606,7 @@ mod prim_pointer {} /// println!("array[{}] = {}", i, x); /// } /// -/// // You can explicitly iterate an array by value using -/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`: +/// // You can explicitly iterate an array by value using `IntoIterator::into_iter` /// for item in IntoIterator::into_iter(array).enumerate() { /// let (i, x): (usize, i32) = item; /// println!("array[{}] = {}", i, x); |
