sized_chunks/sized_chunk/
iter.rs

1use core::iter::FusedIterator;
2
3use super::Chunk;
4use crate::types::ChunkLength;
5
6/// A consuming iterator over the elements of a `Chunk`.
7pub struct Iter<A, N>
8where
9    N: ChunkLength<A>,
10{
11    pub(crate) chunk: Chunk<A, N>,
12}
13
14impl<A, N> Iterator for Iter<A, N>
15where
16    N: ChunkLength<A>,
17{
18    type Item = A;
19    fn next(&mut self) -> Option<Self::Item> {
20        if self.chunk.is_empty() {
21            None
22        } else {
23            Some(self.chunk.pop_front())
24        }
25    }
26
27    fn size_hint(&self) -> (usize, Option<usize>) {
28        (self.chunk.len(), Some(self.chunk.len()))
29    }
30}
31
32impl<A, N> DoubleEndedIterator for Iter<A, N>
33where
34    N: ChunkLength<A>,
35{
36    fn next_back(&mut self) -> Option<Self::Item> {
37        if self.chunk.is_empty() {
38            None
39        } else {
40            Some(self.chunk.pop_back())
41        }
42    }
43}
44
45impl<A, N> ExactSizeIterator for Iter<A, N> where N: ChunkLength<A> {}
46
47impl<A, N> FusedIterator for Iter<A, N> where N: ChunkLength<A> {}
48
49/// A draining iterator over the elements of a `Chunk`.
50///
51/// "Draining" means that as the iterator yields each element, it's removed from
52/// the `Chunk`. When the iterator terminates, the chunk will be empty. This is
53/// different from the consuming iterator `Iter` in that `Iter` will take
54/// ownership of the `Chunk` and discard it when you're done iterating, while
55/// `Drain` leaves you still owning the drained `Chunk`.
56pub struct Drain<'a, A, N>
57where
58    N: ChunkLength<A>,
59{
60    pub(crate) chunk: &'a mut Chunk<A, N>,
61}
62
63impl<'a, A, N> Iterator for Drain<'a, A, N>
64where
65    A: 'a,
66    N: ChunkLength<A> + 'a,
67{
68    type Item = A;
69
70    fn next(&mut self) -> Option<Self::Item> {
71        if self.chunk.is_empty() {
72            None
73        } else {
74            Some(self.chunk.pop_front())
75        }
76    }
77
78    fn size_hint(&self) -> (usize, Option<usize>) {
79        (self.chunk.len(), Some(self.chunk.len()))
80    }
81}
82
83impl<'a, A, N> DoubleEndedIterator for Drain<'a, A, N>
84where
85    A: 'a,
86    N: ChunkLength<A> + 'a,
87{
88    fn next_back(&mut self) -> Option<Self::Item> {
89        if self.chunk.is_empty() {
90            None
91        } else {
92            Some(self.chunk.pop_back())
93        }
94    }
95}
96
97impl<'a, A, N> ExactSizeIterator for Drain<'a, A, N>
98where
99    A: 'a,
100    N: ChunkLength<A> + 'a,
101{
102}
103
104impl<'a, A, N> FusedIterator for Drain<'a, A, N>
105where
106    A: 'a,
107    N: ChunkLength<A> + 'a,
108{
109}