@@ -34,30 +34,23 @@ pub struct IntoIter<T, const N: usize> {
34
34
alive : Range < usize > ,
35
35
}
36
36
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.
47
48
///
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.
52
51
///
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 {
61
54
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
62
55
// promise:
63
56
//
@@ -76,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> {
76
69
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
77
70
// as a different type, then forget `array` so that it is not dropped.
78
71
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 ) ;
81
74
iter
82
75
}
83
76
}
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
+ }
84
86
85
87
/// Returns an immutable slice of all elements that have not been yielded
86
88
/// yet.
0 commit comments