@@ -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.
0 commit comments