src/maputil.rs

changeset 204
016122e0384b
parent 94
1f19c6bbf07b
equal deleted inserted replaced
203:ad16acf47726 204:016122e0384b
78 Res, 78 Res,
79 const N : usize 79 const N : usize
80 >( 80 >(
81 $var0 : $ctype0, 81 $var0 : $ctype0,
82 $($var : $ctype,)* 82 $($var : $ctype,)*
83 f : impl Fn($etype0, $($etype),*) -> Res 83 mut f : impl FnMut($etype0, $($etype),*) -> Res
84 ) -> [Res; N] { 84 ) -> [Res; N] {
85 let zipit = izip!($var0.fl_iter(), $($var.fl_iter()),*); 85 let zipit = izip!($var0.fl_iter(), $($var.fl_iter()),*);
86 let map = zipit.map(|tuple_or_singleton!($var0, $($var),*)| f($var0, $($var),*)); 86 let map = zipit.map(|tuple_or_singleton!($var0, $($var),*)| f($var0, $($var),*));
87 collect_into_array_unchecked(map) 87 collect_into_array_unchecked(map)
88 } 88 }
97 Res, 97 Res,
98 const N : usize 98 const N : usize
99 >( 99 >(
100 $var0 : $ctype0, 100 $var0 : $ctype0,
101 $($var : $ctype,)* 101 $($var : $ctype,)*
102 f : impl Fn(usize, $etype0, $($etype),*) -> Res 102 mut f : impl FnMut(usize, $etype0, $($etype),*) -> Res
103 ) -> [Res; N] { 103 ) -> [Res; N] {
104 let zipit = (0..N).zip(izip!($var0.fl_iter(), $($var.fl_iter()),*)); 104 let zipit = (0..N).zip(izip!($var0.fl_iter(), $($var.fl_iter()),*));
105 let map = zipit.map(|(i, tuple_or_singleton!($var0, $($var),*))| f(i, $var0, $($var),*)); 105 let map = zipit.map(|(i, tuple_or_singleton!($var0, $($var),*))| f(i, $var0, $($var),*));
106 collect_into_array_unchecked(map) 106 collect_into_array_unchecked(map)
107 } 107 }
261 = IndexMap<Self, &'a A, B, F> 261 = IndexMap<Self, &'a A, B, F>
262 where 262 where
263 F: Fn(usize, &'a A) -> B; 263 F: Fn(usize, &'a A) -> B;
264 #[inline] 264 #[inline]
265 fn indexmap<B, F: Fn(usize, &'a A) -> B>(self, f: F) -> Self::Output<B, F> { 265 fn indexmap<B, F: Fn(usize, &'a A) -> B>(self, f: F) -> Self::Output<B, F> {
266 IndexMap { 266 IndexMap { iter: self, j: 0, f }
267 iter: self,
268 j: 0,
269 f,
270 }
271 } 267 }
272 } 268 }
273 269
274 impl<A, J: Copy, const N: usize> FoldMappable<A, J> for std::array::IntoIter<A, N> { 270 impl<A, J: Copy, const N: usize> FoldMappable<A, J> for std::array::IntoIter<A, N> {
275 type Output<B, F> 271 type Output<B, F>
287 = IndexMap<Self, A, B, F> 283 = IndexMap<Self, A, B, F>
288 where 284 where
289 F: Fn(usize, A) -> B; 285 F: Fn(usize, A) -> B;
290 #[inline] 286 #[inline]
291 fn indexmap<B, F: Fn(usize, A) -> B>(self, f: F) -> Self::Output<B, F> { 287 fn indexmap<B, F: Fn(usize, A) -> B>(self, f: F) -> Self::Output<B, F> {
292 IndexMap { 288 IndexMap { iter: self, j: 0, f }
293 iter: self,
294 j: 0,
295 f,
296 }
297 } 289 }
298 } 290 }
299 291
300 impl<A, J: Copy, const N: usize> FoldMappable<A, J> for [A; N] { 292 impl<A, J: Copy, const N: usize> FoldMappable<A, J> for [A; N] {
301 type Output<B, F> 293 type Output<B, F>
380 } 372 }
381 } 373 }
382 } 374 }
383 375
384 let mut array = [const { MaybeUninit::uninit() }; N]; 376 let mut array = [const { MaybeUninit::uninit() }; N];
385 let mut guard = Guard { 377 let mut guard = Guard { array_mut: &mut array, initialized: 0 };
386 array_mut: &mut array,
387 initialized: 0,
388 };
389 378
390 while let Some(item) = iter.next() { 379 while let Some(item) = iter.next() {
391 // SAFETY: `guard.initialized` starts at 0, is increased by one in the 380 // SAFETY: `guard.initialized` starts at 0, is increased by one in the
392 // loop and the loop is aborted once it reaches N (which is 381 // loop and the loop is aborted once it reaches N (which is
393 // `array.len()`). 382 // `array.len()`).

mercurial