@@ -34,11 +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
- #[ 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 {
42
54
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
43
55
// promise:
44
56
//
@@ -57,11 +69,20 @@ impl<T, const N: usize> IntoIter<T, N> {
57
69
// Until then, we can use `mem::transmute_copy` to create a bitwise copy
58
70
// as a different type, then forget `array` so that it is not dropped.
59
71
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 ) ;
62
74
iter
63
75
}
64
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.57.0" , reason = "use `IntoIterator::into_iter` instead" ) ]
83
+ pub fn new ( array : [ T ; N ] ) -> Self {
84
+ IntoIterator :: into_iter ( array)
85
+ }
65
86
66
87
/// Returns an immutable slice of all elements that have not been yielded
67
88
/// yet.
0 commit comments