Skip to main content

micro_moka/unsync/
iter.rs

1use super::SlabEntry;
2use std::iter::FusedIterator;
3
4pub struct Iter<'i, K, V> {
5    inner: std::slice::Iter<'i, Option<SlabEntry<K, V>>>,
6    remaining: usize,
7}
8
9impl<'i, K, V> Iter<'i, K, V> {
10    pub(crate) fn new(entries: &'i [Option<SlabEntry<K, V>>], remaining: usize) -> Self {
11        Self {
12            inner: entries.iter(),
13            remaining,
14        }
15    }
16}
17
18impl<'i, K, V> Iterator for Iter<'i, K, V> {
19    type Item = (&'i K, &'i V);
20
21    fn next(&mut self) -> Option<Self::Item> {
22        loop {
23            match self.inner.next() {
24                Some(Some(entry)) => {
25                    self.remaining -= 1;
26                    return Some((&entry.key, &entry.value));
27                }
28                Some(None) => continue,
29                None => return None,
30            }
31        }
32    }
33
34    fn size_hint(&self) -> (usize, Option<usize>) {
35        (self.remaining, Some(self.remaining))
36    }
37}
38
39impl<K, V> ExactSizeIterator for Iter<'_, K, V> {}
40impl<K, V> FusedIterator for Iter<'_, K, V> {}