sized_chunks/inline_array/
iter.rs

1use core::iter::FusedIterator;
2
3use crate::InlineArray;
4
5/// A consuming iterator over the elements of an `InlineArray`.
6pub struct Iter<A, T> {
7    pub(crate) array: InlineArray<A, T>,
8}
9
10impl<A, T> Iterator for Iter<A, T> {
11    type Item = A;
12
13    fn next(&mut self) -> Option<Self::Item> {
14        self.array.remove(0)
15    }
16
17    fn size_hint(&self) -> (usize, Option<usize>) {
18        (self.array.len(), Some(self.array.len()))
19    }
20}
21
22impl<A, T> DoubleEndedIterator for Iter<A, T> {
23    fn next_back(&mut self) -> Option<Self::Item> {
24        self.array.pop()
25    }
26}
27
28impl<A, T> ExactSizeIterator for Iter<A, T> {}
29
30impl<A, T> FusedIterator for Iter<A, T> {}
31
32/// A draining iterator over the elements of an `InlineArray`.
33///
34/// "Draining" means that as the iterator yields each element, it's removed from
35/// the `InlineArray`. When the iterator terminates, the array will be empty.
36/// This is different from the consuming iterator `Iter` in that `Iter` will
37/// take ownership of the `InlineArray` and discard it when you're done
38/// iterating, while `Drain` leaves you still owning the drained `InlineArray`.
39pub struct Drain<'a, A, T> {
40    pub(crate) array: &'a mut InlineArray<A, T>,
41}
42
43impl<'a, A, T> Iterator for Drain<'a, A, T> {
44    type Item = A;
45
46    fn next(&mut self) -> Option<Self::Item> {
47        self.array.remove(0)
48    }
49
50    fn size_hint(&self) -> (usize, Option<usize>) {
51        (self.array.len(), Some(self.array.len()))
52    }
53}
54
55impl<'a, A, T> DoubleEndedIterator for Drain<'a, A, T> {
56    fn next_back(&mut self) -> Option<Self::Item> {
57        self.array.pop()
58    }
59}
60
61impl<'a, A, T> ExactSizeIterator for Drain<'a, A, T> {}
62
63impl<'a, A, T> FusedIterator for Drain<'a, A, T> {}