| 1 |
//! Support for constructing arrays using a provided iterator function and other iterator-related |
| 2 |
//! functionality. |
| 3 |
|
| 4 |
use crate::{Array, ArraySize}; |
| 5 |
use core::{ |
| 6 |
fmt, |
| 7 |
slice::{Iter, IterMut}, |
| 8 |
}; |
| 9 |
|
| 10 |
/// Couldn't construct an array from an iterator because the number of items in the iterator |
| 11 |
/// didn't match the array size. |
| 12 |
#[derive(Clone, Copy, Debug)] |
| 13 |
pub struct TryFromIteratorError; |
| 14 |
|
| 15 |
impl fmt::Display for TryFromIteratorError { |
| 16 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 17 |
f.write_str("iterator did not contain the correct number of items for the array size") |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl core::error::Error for TryFromIteratorError {} |
| 22 |
|
| 23 |
impl<T, U> Array<T, U> |
| 24 |
where |
| 25 |
U: ArraySize, |
| 26 |
{ |
| 27 |
/// Construct an array from the given iterator, returning [`TryFromIteratorError`] in the event |
| 28 |
/// that the number of items in the iterator does not match the array size. |
| 29 |
/// |
| 30 |
/// # Errors |
| 31 |
/// |
| 32 |
/// Returns [`TryFromIteratorError`] in the event the iterator does not return a number of |
| 33 |
/// items which is exactly equal to the array size. |
| 34 |
pub fn try_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Result<Self, TryFromIteratorError> { |
| 35 |
let mut iter = iter.into_iter(); |
| 36 |
let ret = Self::try_from_fn(|_| iter.next().ok_or(TryFromIteratorError))?; |
| 37 |
|
| 38 |
match iter.next() { |
| 39 |
None => Ok(ret), |
| 40 |
Some(_) => Err(TryFromIteratorError), |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl<T, U> FromIterator<T> for Array<T, U> |
| 46 |
where |
| 47 |
U: ArraySize, |
| 48 |
{ |
| 49 |
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { |
| 50 |
let mut iter = iter.into_iter(); |
| 51 |
let ret = Self::from_fn(|_| { |
| 52 |
iter.next() |
| 53 |
.expect("iterator should have enough items to fill array") |
| 54 |
}); |
| 55 |
|
| 56 |
assert!( |
| 57 |
iter.next().is_none(), |
| 58 |
"too many items in iterator to fit in array" |
| 59 |
); |
| 60 |
|
| 61 |
ret |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<T, U> IntoIterator for Array<T, U> |
| 66 |
where |
| 67 |
U: ArraySize, |
| 68 |
{ |
| 69 |
type Item = T; |
| 70 |
type IntoIter = <U::ArrayType<T> as IntoIterator>::IntoIter; |
| 71 |
|
| 72 |
/// Creates a consuming iterator, that is, one that moves each value out of the array (from |
| 73 |
/// start to end). |
| 74 |
/// |
| 75 |
/// The array cannot be used after calling this unless `T` implements `Copy`, so the whole |
| 76 |
/// array is copied. |
| 77 |
#[inline] |
| 78 |
fn into_iter(self) -> Self::IntoIter { |
| 79 |
self.0.into_iter() |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
impl<'a, T, U> IntoIterator for &'a Array<T, U> |
| 84 |
where |
| 85 |
U: ArraySize, |
| 86 |
{ |
| 87 |
type Item = &'a T; |
| 88 |
type IntoIter = Iter<'a, T>; |
| 89 |
|
| 90 |
#[inline] |
| 91 |
fn into_iter(self) -> Iter<'a, T> { |
| 92 |
self.iter() |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl<'a, T, U> IntoIterator for &'a mut Array<T, U> |
| 97 |
where |
| 98 |
U: ArraySize, |
| 99 |
{ |
| 100 |
type Item = &'a mut T; |
| 101 |
type IntoIter = IterMut<'a, T>; |
| 102 |
|
| 103 |
#[inline] |
| 104 |
fn into_iter(self) -> IterMut<'a, T> { |
| 105 |
self.iter_mut() |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#[cfg(test)] |
| 110 |
mod tests { |
| 111 |
use crate::{Array, sizes::U3}; |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn mut_array_ref_into_iterator() { |
| 115 |
let array_ref: &mut Array<u8, U3> = &mut Array([1, 2, 3]); |
| 116 |
let mut iter = array_ref.into_iter(); |
| 117 |
|
| 118 |
for i in 1..=3 { |
| 119 |
assert_eq!(iter.next().copied(), Some(i)); |
| 120 |
} |
| 121 |
|
| 122 |
assert_eq!(iter.next(), None); |
| 123 |
} |
| 124 |
|
| 125 |
#[cfg(feature = "alloc")] |
| 126 |
#[test] |
| 127 |
fn display_try_from_iterator_error() { |
| 128 |
use super::TryFromIteratorError; |
| 129 |
use alloc::string::ToString; |
| 130 |
let _ = TryFromIteratorError.to_string(); |
| 131 |
} |
| 132 |
} |