src/maputil.rs

changeset 90
b3c35d16affe
parent 55
7b2ee3e84c5f
child 91
db870f2a2cde
equal deleted inserted replaced
25:d14c877e14b7 90:b3c35d16affe
1 /*! 1 /*!
2 Utilities for mapping over various container types. 2 Utilities for mapping over various container types.
3 */ 3 */
4 4
5 #[cfg(feature = "nightly")]
5 use std::mem::MaybeUninit; 6 use std::mem::MaybeUninit;
6 use itertools::izip; 7 use itertools::izip;
7 8
8 /// Trait for a fixed-length container type. 9 /// Trait for a fixed-length container type.
9 /// 10 ///
320 /// `next` at most `N` times, the iterator can still be used afterwards to 321 /// `next` at most `N` times, the iterator can still be used afterwards to
321 /// retrieve the remaining items. 322 /// retrieve the remaining items.
322 /// 323 ///
323 /// If `iter.next()` panicks, all items already yielded by the iterator are 324 /// If `iter.next()` panicks, all items already yielded by the iterator are
324 /// dropped. 325 /// dropped.
326 #[cfg(feature = "nightly")]
325 #[inline] 327 #[inline]
326 pub(crate) fn collect_into_array_unchecked<T, I : Iterator<Item=T>, const N: usize>(mut iter: I) -> [T; N] 328 pub(crate) fn collect_into_array_unchecked<
329 T,
330 I : Iterator<Item=T>,
331 const N: usize
332 >(mut iter: I) -> [T; N]
327 { 333 {
328 if N == 0 { 334 if N == 0 {
329 // SAFETY: An empty array is always inhabited and has no validity invariants. 335 // SAFETY: An empty array is always inhabited and has no validity invariants.
330 return unsafe { core::mem::zeroed() }; 336 return unsafe { core::mem::zeroed() };
331 } 337 }
373 } 379 }
374 380
375 unreachable!("Something went wrong with iterator length") 381 unreachable!("Something went wrong with iterator length")
376 } 382 }
377 383
384 #[cfg(not(feature = "nightly"))]
385 #[inline]
386 pub(crate) fn collect_into_array_unchecked<
387 T,
388 I : Iterator<Item=T>,
389 const N: usize
390 >(iter: I) -> [T; N]
391 {
392 match iter.collect::<Vec<T>>().try_into() {
393 Ok(a) => a,
394 Err(_) => panic!("collect_into_array failure: should not happen"),
395 }
396 }
378 397
379 #[cfg(test)] 398 #[cfg(test)]
380 mod tests { 399 mod tests {
381 use super::*; 400 use super::*;
382 401

mercurial