@@ -13,17 +13,14 @@ where
1313 /// If the mapping function panics, any already initialized elements in the new array will
1414 /// be dropped, AND any unused elements in the source array will also be dropped.
1515 #[ inline( always) ]
16- pub fn map < O , F > ( self , mut f : F ) -> Array < O , U >
16+ pub fn map < O , F > ( self , f : F ) -> Array < O , U >
1717 where
1818 F : FnMut ( T ) -> O ,
1919 {
20- let mut out = Array :: uninit ( ) ;
21-
22- for ( i, s) in self . into_iter ( ) . enumerate ( ) {
23- out[ i] . write ( f ( s) ) ;
24- }
25-
26- unsafe { out. assume_init ( ) }
20+ Array :: from_iter ( Mapper {
21+ inner : self . into_iter ( ) ,
22+ f,
23+ } )
2724 }
2825
2926 /// Combines two `Array` instances and iterates through both of them, initialization a new
@@ -32,21 +29,16 @@ where
3229 /// If the mapping function panics, any already initialized elements in the new array will
3330 /// be dropped, AND any unused elements in the source arrays will also be dropped.
3431 #[ inline( always) ]
35- pub fn zip < Rhs , F , O > ( self , rhs : Array < Rhs , U > , mut f : F ) -> Array < O , U >
32+ pub fn zip < Rhs , F , O > ( self , rhs : Array < Rhs , U > , f : F ) -> Array < O , U >
3633 where
3734 U : ArraySize ,
3835 F : FnMut ( T , Rhs ) -> O ,
3936 {
40- let mut out = Array :: uninit ( ) ;
41-
42- let mut s = self . into_iter ( ) . enumerate ( ) ;
43- let mut r = rhs. into_iter ( ) ;
44-
45- while let ( Some ( ( i, s) ) , Some ( r) ) = ( s. next ( ) , r. next ( ) ) {
46- out[ i] . write ( f ( s, r) ) ;
47- }
48-
49- unsafe { out. assume_init ( ) }
37+ Array :: from_iter ( Zipper {
38+ inner : self . into_iter ( ) ,
39+ rhs : rhs. into_iter ( ) ,
40+ f,
41+ } )
5042 }
5143
5244 /// Folds (or reduces) a sequence of data into a single value.
6456 init
6557 }
6658}
59+
60+ struct Mapper < I , F > {
61+ inner : I ,
62+ f : F ,
63+ }
64+
65+ impl < I , T , O , F > Iterator for Mapper < I , F >
66+ where
67+ I : Iterator < Item = T > ,
68+ F : FnMut ( T ) -> O ,
69+ {
70+ type Item = O ;
71+
72+ #[ inline( always) ]
73+ fn next ( & mut self ) -> Option < Self :: Item > {
74+ Some ( ( self . f ) ( self . inner . next ( ) ?) )
75+ }
76+ }
77+
78+ struct Zipper < I , R , F > {
79+ inner : I ,
80+ rhs : R ,
81+ f : F ,
82+ }
83+
84+ impl < I , T , R , RT , O , F > Iterator for Zipper < I , R , F >
85+ where
86+ I : Iterator < Item = T > ,
87+ R : Iterator < Item = RT > ,
88+ F : FnMut ( T , RT ) -> O ,
89+ {
90+ type Item = O ;
91+
92+ #[ inline( always) ]
93+ fn next ( & mut self ) -> Option < Self :: Item > {
94+ Some ( ( self . f ) ( self . inner . next ( ) ?, self . rhs . next ( ) ?) )
95+ }
96+ }
0 commit comments