Skip to content

Commit 4a2e16c

Browse files
committed
Auto merge of rust-lang#88611 - m-ou-se:array-into-iter-new-deprecate, r=joshtriplett
Deprecate array::IntoIter::new.
2 parents 66a4788 + 2a964d5 commit 4a2e16c

File tree

11 files changed

+42
-64
lines changed

11 files changed

+42
-64
lines changed

alloc/src/collections/binary_heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
15001500
/// }
15011501
/// ```
15021502
fn from(arr: [T; N]) -> Self {
1503-
core::array::IntoIter::new(arr).collect()
1503+
Self::from_iter(arr)
15041504
}
15051505
}
15061506

alloc/src/collections/btree/map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,11 +1305,11 @@ impl<K, V> BTreeMap<K, V> {
13051305
pub(crate) fn bulk_build_from_sorted_iter<I>(iter: I) -> Self
13061306
where
13071307
K: Ord,
1308-
I: Iterator<Item = (K, V)>,
1308+
I: IntoIterator<Item = (K, V)>,
13091309
{
13101310
let mut root = Root::new();
13111311
let mut length = 0;
1312-
root.bulk_push(DedupSortedIter::new(iter), &mut length);
1312+
root.bulk_push(DedupSortedIter::new(iter.into_iter()), &mut length);
13131313
BTreeMap { root: Some(root), length }
13141314
}
13151315
}
@@ -1944,7 +1944,7 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
19441944

19451945
// use stable sort to preserve the insertion order.
19461946
inputs.sort_by(|a, b| a.0.cmp(&b.0));
1947-
BTreeMap::bulk_build_from_sorted_iter(inputs.into_iter())
1947+
BTreeMap::bulk_build_from_sorted_iter(inputs)
19481948
}
19491949
}
19501950

@@ -2061,7 +2061,7 @@ impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> {
20612061

20622062
// use stable sort to preserve the insertion order.
20632063
arr.sort_by(|a, b| a.0.cmp(&b.0));
2064-
BTreeMap::bulk_build_from_sorted_iter(core::array::IntoIter::new(arr))
2064+
BTreeMap::bulk_build_from_sorted_iter(arr)
20652065
}
20662066
}
20672067

alloc/src/collections/btree/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> {
11061106

11071107
// use stable sort to preserve the insertion order.
11081108
arr.sort();
1109-
let iter = core::array::IntoIter::new(arr).map(|k| (k, ()));
1109+
let iter = IntoIterator::into_iter(arr).map(|k| (k, ()));
11101110
let map = BTreeMap::bulk_build_from_sorted_iter(iter);
11111111
BTreeSet { map }
11121112
}

alloc/src/collections/linked_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ impl<T, const N: usize> From<[T; N]> for LinkedList<T> {
19611961
/// assert_eq!(list1, list2);
19621962
/// ```
19631963
fn from(arr: [T; N]) -> Self {
1964-
core::array::IntoIter::new(arr).collect()
1964+
Self::from_iter(arr)
19651965
}
19661966
}
19671967

core/src/array/iter.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,23 @@ pub struct IntoIter<T, const N: usize> {
3434
alive: Range<usize>,
3535
}
3636

37-
impl<T, const N: usize> IntoIter<T, N> {
38-
/// Creates a new iterator over the given `array`.
39-
///
40-
/// *Note*: this method might be deprecated in the future,
41-
/// since [`IntoIterator`] is now implemented for arrays.
42-
///
43-
/// # Examples
44-
///
45-
/// ```
46-
/// use std::array;
37+
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
38+
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
39+
// so those calls will still resolve to the slice implementation, by reference.
40+
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
41+
impl<T, const N: usize> IntoIterator for [T; N] {
42+
type Item = T;
43+
type IntoIter = IntoIter<T, N>;
44+
45+
/// Creates a consuming iterator, that is, one that moves each value out of
46+
/// the array (from start to end). The array cannot be used after calling
47+
/// this unless `T` implements `Copy`, so the whole array is copied.
4748
///
48-
/// for value in array::IntoIter::new([1, 2, 3, 4, 5]) {
49-
/// // The type of `value` is an `i32` here, instead of `&i32`
50-
/// let _: i32 = value;
51-
/// }
49+
/// Arrays have special behavior when calling `.into_iter()` prior to the
50+
/// 2021 edition -- see the [array] Editions section for more information.
5251
///
53-
/// // Since Rust 1.53, arrays implement IntoIterator directly:
54-
/// for value in [1, 2, 3, 4, 5] {
55-
/// // The type of `value` is an `i32` here, instead of `&i32`
56-
/// let _: i32 = value;
57-
/// }
58-
/// ```
59-
#[stable(feature = "array_value_iter", since = "1.51.0")]
60-
pub fn new(array: [T; N]) -> Self {
52+
/// [array]: prim@array
53+
fn into_iter(self) -> Self::IntoIter {
6154
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
6255
// promise:
6356
//
@@ -76,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> {
7669
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
7770
// as a different type, then forget `array` so that it is not dropped.
7871
unsafe {
79-
let iter = Self { data: mem::transmute_copy(&array), alive: 0..N };
80-
mem::forget(array);
72+
let iter = IntoIter { data: mem::transmute_copy(&self), alive: 0..N };
73+
mem::forget(self);
8174
iter
8275
}
8376
}
77+
}
78+
79+
impl<T, const N: usize> IntoIter<T, N> {
80+
/// Creates a new iterator over the given `array`.
81+
#[stable(feature = "array_value_iter", since = "1.51.0")]
82+
#[rustc_deprecated(since = "1.59.0", reason = "use `IntoIterator::into_iter` instead")]
83+
pub fn new(array: [T; N]) -> Self {
84+
IntoIterator::into_iter(array)
85+
}
8486

8587
/// Returns an immutable slice of all elements that have not been yielded
8688
/// yet.

core/src/array/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,6 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
243243
}
244244
}
245245

246-
// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
247-
// hides this implementation from explicit `.into_iter()` calls on editions < 2021,
248-
// so those calls will still resolve to the slice implementation, by reference.
249-
#[stable(feature = "array_into_iter_impl", since = "1.53.0")]
250-
impl<T, const N: usize> IntoIterator for [T; N] {
251-
type Item = T;
252-
type IntoIter = IntoIter<T, N>;
253-
254-
/// Creates a consuming iterator, that is, one that moves each value out of
255-
/// the array (from start to end). The array cannot be used after calling
256-
/// this unless `T` implements `Copy`, so the whole array is copied.
257-
///
258-
/// Arrays have special behavior when calling `.into_iter()` prior to the
259-
/// 2021 edition -- see the [array] Editions section for more information.
260-
///
261-
/// [array]: prim@array
262-
fn into_iter(self) -> Self::IntoIter {
263-
IntoIter::new(self)
264-
}
265-
}
266-
267246
#[stable(feature = "rust1", since = "1.0.0")]
268247
impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
269248
type Item = &'a T;

core/src/primitive_docs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,7 @@ mod prim_pointer {}
606606
/// println!("array[{}] = {}", i, x);
607607
/// }
608608
///
609-
/// // You can explicitly iterate an array by value using
610-
/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`:
609+
/// // You can explicitly iterate an array by value using `IntoIterator::into_iter`
611610
/// for item in IntoIterator::into_iter(array).enumerate() {
612611
/// let (i, x): (usize, i32) = item;
613612
/// println!("array[{}] = {}", i, x);

core/tests/iter/adapters/flatten.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use core::array;
32
use core::iter::*;
43

54
#[test]
@@ -134,7 +133,7 @@ fn test_double_ended_flatten() {
134133
#[test]
135134
fn test_trusted_len_flatten() {
136135
fn assert_trusted_len<T: TrustedLen>(_: &T) {}
137-
let mut iter = array::IntoIter::new([[0; 3]; 4]).flatten();
136+
let mut iter = IntoIterator::into_iter([[0; 3]; 4]).flatten();
138137
assert_trusted_len(&iter);
139138

140139
assert_eq!(iter.size_hint(), (12, Some(12)));
@@ -143,21 +142,21 @@ fn test_trusted_len_flatten() {
143142
iter.next_back();
144143
assert_eq!(iter.size_hint(), (10, Some(10)));
145144

146-
let iter = array::IntoIter::new([[(); usize::MAX]; 1]).flatten();
145+
let iter = IntoIterator::into_iter([[(); usize::MAX]; 1]).flatten();
147146
assert_eq!(iter.size_hint(), (usize::MAX, Some(usize::MAX)));
148147

149-
let iter = array::IntoIter::new([[(); usize::MAX]; 2]).flatten();
148+
let iter = IntoIterator::into_iter([[(); usize::MAX]; 2]).flatten();
150149
assert_eq!(iter.size_hint(), (usize::MAX, None));
151150

152151
let mut a = [(); 10];
153152
let mut b = [(); 10];
154153

155-
let iter = array::IntoIter::new([&mut a, &mut b]).flatten();
154+
let iter = IntoIterator::into_iter([&mut a, &mut b]).flatten();
156155
assert_trusted_len(&iter);
157156
assert_eq!(iter.size_hint(), (20, Some(20)));
158157
core::mem::drop(iter);
159158

160-
let iter = array::IntoIter::new([&a, &b]).flatten();
159+
let iter = IntoIterator::into_iter([&a, &b]).flatten();
161160
assert_trusted_len(&iter);
162161
assert_eq!(iter.size_hint(), (20, Some(20)));
163162

std/src/collections/hash/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ where
11861186
/// assert_eq!(map1, map2);
11871187
/// ```
11881188
fn from(arr: [(K, V); N]) -> Self {
1189-
crate::array::IntoIter::new(arr).collect()
1189+
Self::from_iter(arr)
11901190
}
11911191
}
11921192

std/src/collections/hash/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ where
10221022
/// assert_eq!(set1, set2);
10231023
/// ```
10241024
fn from(arr: [T; N]) -> Self {
1025-
crate::array::IntoIter::new(arr).collect()
1025+
Self::from_iter(arr)
10261026
}
10271027
}
10281028

0 commit comments

Comments
 (0)