hybrid_array/
iter.rs

1//! Support for constructing arrays using a provided iterator function and other iterator-related
2//! functionality.
3
4use crate::{Array, ArraySize};
5use 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)]
13pub struct TryFromIteratorError;
14
15impl 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
21impl core::error::Error for TryFromIteratorError {}
22
23impl<T, U> Array<T, U>
24where
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
45impl<T, U> FromIterator<T> for Array<T, U>
46where
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
65impl<T, U> IntoIterator for Array<T, U>
66where
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
83impl<'a, T, U> IntoIterator for &'a Array<T, U>
84where
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
96impl<'a, T, U> IntoIterator for &'a mut Array<T, U>
97where
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}