1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::cursor::Cursor;
use crate::entry::ItemEntry;
use crate::mark::XMark;

/// An iterator over a range of entries in an [`XArray`].
///
/// The typical way to obtain a `Range` instance is to call [`XArray::range`].
///
/// [`XArray`]: crate::XArray
/// [`XArray::range`]: crate::XArray::range
pub struct Range<'a, I, M>
where
    I: ItemEntry,
    M: Into<XMark>,
{
    cursor: Cursor<'a, I, M>,
    end: u64,
}

impl<'a, I: ItemEntry, M: Into<XMark>> Range<'a, I, M> {
    pub(super) fn new(cursor: Cursor<'a, I, M>, end: u64) -> Self {
        Range { cursor, end }
    }
}

impl<'a, I: ItemEntry, M: Into<XMark>> core::iter::Iterator for Range<'a, I, M> {
    type Item = (u64, I::Ref<'a>);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.cursor.index() >= self.end {
                return None;
            }

            let item = self.cursor.load();
            if item.is_none() {
                self.cursor.next();
                continue;
            }

            let res = item.map(|item| (self.cursor.index(), item));
            self.cursor.next();
            return res;
        }
    }
}