orx_concurrent_iter/enumerate/
con_iter.rs

1use super::chunk_puller::EnumeratedChunkPuller;
2use crate::ConcurrentIter;
3
4/// An enumerated version of a concurrent iterator which additionally yields
5/// the index of elements in the source collection.
6///
7/// It can be created by calling [`enumerate`] on a concurrent iterator.
8///
9/// [`enumerate`]: crate::ConcurrentIter::enumerate
10///
11/// # Examples
12///
13/// ```
14/// use orx_concurrent_iter::*;
15///
16/// let vec = vec!['x', 'y'];
17///
18/// let con_iter = vec.con_iter().enumerate();
19/// assert_eq!(con_iter.next(), Some((0, &'x')));
20/// assert_eq!(con_iter.next(), Some((1, &'y')));
21/// assert_eq!(con_iter.next(), None);
22/// ```
23pub struct Enumerate<I>
24where
25    I: ConcurrentIter,
26{
27    iter: I,
28}
29
30impl<I> Enumerate<I>
31where
32    I: ConcurrentIter,
33{
34    pub(crate) fn new(iter: I) -> Self {
35        Self { iter }
36    }
37}
38
39impl<I> ConcurrentIter for Enumerate<I>
40where
41    I: ConcurrentIter,
42{
43    type Item = (usize, I::Item);
44
45    type SequentialIter = core::iter::Enumerate<I::SequentialIter>;
46
47    type ChunkPuller<'i>
48        = EnumeratedChunkPuller<I::ChunkPuller<'i>>
49    where
50        Self: 'i;
51
52    fn into_seq_iter(self) -> Self::SequentialIter {
53        self.iter.into_seq_iter().enumerate()
54    }
55
56    fn skip_to_end(&self) {
57        self.iter.skip_to_end();
58    }
59
60    fn next(&self) -> Option<Self::Item> {
61        self.iter.next_with_idx()
62    }
63
64    fn next_with_idx(&self) -> Option<(usize, Self::Item)> {
65        self.iter.next_with_idx().map(|(i, x)| (i, (i, x)))
66    }
67
68    fn size_hint(&self) -> (usize, Option<usize>) {
69        self.iter.size_hint()
70    }
71
72    fn chunk_puller(&self, chunk_size: usize) -> Self::ChunkPuller<'_> {
73        Self::ChunkPuller::new(self.iter.chunk_puller(chunk_size))
74    }
75}