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/core/src/array | |
| 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/core/src/array')
| -rw-r--r-- | library/core/src/array/iter.rs | 50 | ||||
| -rw-r--r-- | library/core/src/array/mod.rs | 21 |
2 files changed, 26 insertions, 45 deletions
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; |
