@@ -17,9 +17,12 @@ use crate::ops::{
1717} ;
1818use crate :: slice:: { Iter , IterMut } ;
1919
20+ mod drain;
2021mod equality;
2122mod iter;
2223
24+ pub ( crate ) use drain:: drain_array_with;
25+
2326#[ stable( feature = "array_value_iter" , since = "1.51.0" ) ]
2427pub use iter:: IntoIter ;
2528
@@ -513,9 +516,12 @@ impl<T, const N: usize> [T; N] {
513516 where
514517 F : FnMut ( T ) -> U ,
515518 {
516- // SAFETY: we know for certain that this iterator will yield exactly `N`
517- // items.
518- unsafe { collect_into_array_unchecked ( & mut IntoIterator :: into_iter ( self ) . map ( f) ) }
519+ drain_array_with ( self , |iter| {
520+ let mut iter = iter. map ( f) ;
521+ // SAFETY: we know for certain that this iterator will yield exactly `N`
522+ // items.
523+ unsafe { collect_into_array_unchecked ( & mut iter) }
524+ } )
519525 }
520526
521527 /// A fallible function `f` applied to each element on array `self` in order to
@@ -552,9 +558,12 @@ impl<T, const N: usize> [T; N] {
552558 R : Try ,
553559 R :: Residual : Residual < [ R :: Output ; N ] > ,
554560 {
555- // SAFETY: we know for certain that this iterator will yield exactly `N`
556- // items.
557- unsafe { try_collect_into_array_unchecked ( & mut IntoIterator :: into_iter ( self ) . map ( f) ) }
561+ drain_array_with ( self , |iter| {
562+ let mut iter = iter. map ( f) ;
563+ // SAFETY: we know for certain that this iterator will yield exactly `N`
564+ // items.
565+ unsafe { try_collect_into_array_unchecked ( & mut iter) }
566+ } )
558567 }
559568
560569 /// 'Zips up' two arrays into a single array of pairs.
@@ -575,11 +584,14 @@ impl<T, const N: usize> [T; N] {
575584 /// ```
576585 #[ unstable( feature = "array_zip" , issue = "80094" ) ]
577586 pub fn zip < U > ( self , rhs : [ U ; N ] ) -> [ ( T , U ) ; N ] {
578- let mut iter = IntoIterator :: into_iter ( self ) . zip ( rhs) ;
579-
580- // SAFETY: we know for certain that this iterator will yield exactly `N`
581- // items.
582- unsafe { collect_into_array_unchecked ( & mut iter) }
587+ drain_array_with ( self , |lhs| {
588+ drain_array_with ( rhs, |rhs| {
589+ let mut iter = crate :: iter:: zip ( lhs, rhs) ;
590+ // SAFETY: we know for certain that this iterator will yield exactly `N`
591+ // items.
592+ unsafe { collect_into_array_unchecked ( & mut iter) }
593+ } )
594+ } )
583595 }
584596
585597 /// Returns a slice containing the entire array. Equivalent to `&s[..]`.
0 commit comments