Skip to content

Commit b34cf1a

Browse files
committed
Swap body of array::IntoIter::new and IntoIterator::new.
1 parent 911ee94 commit b34cf1a

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

library/core/src/array/iter.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -34,11 +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-
#[stable(feature = "array_value_iter", since = "1.51.0")]
40-
#[rustc_deprecated(since = "1.57.0", reason = "use `IntoIterator::into_iter` instead")]
41-
pub fn new(array: [T; N]) -> Self {
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.
48+
///
49+
/// Arrays have special behavior when calling `.into_iter()` prior to the
50+
/// 2021 edition -- see the [array] Editions section for more information.
51+
///
52+
/// [array]: prim@array
53+
fn into_iter(self) -> Self::IntoIter {
4254
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
4355
// promise:
4456
//
@@ -57,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> {
5769
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
5870
// as a different type, then forget `array` so that it is not dropped.
5971
unsafe {
60-
let iter = Self { data: mem::transmute_copy(&array), alive: 0..N };
61-
mem::forget(array);
72+
let iter = IntoIter { data: mem::transmute_copy(&self), alive: 0..N };
73+
mem::forget(self);
6274
iter
6375
}
6476
}
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.57.0", reason = "use `IntoIterator::into_iter` instead")]
83+
pub fn new(array: [T; N]) -> Self {
84+
IntoIterator::into_iter(array)
85+
}
6586

6687
/// Returns an immutable slice of all elements that have not been yielded
6788
/// yet.

library/core/src/array/mod.rs

-21
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;

0 commit comments

Comments
 (0)