range.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use crate::cursor::Cursor;
  2. use crate::entry::{ItemEntry, ItemRef};
  3. use crate::lock::XLock;
  4. use crate::mark::XMark;
  5. /// An iterator over a sub-range of entries in a XArray.
  6. /// This struct is created by the `range()` method on `XArray`.
  7. pub struct Range<'a, I, L, M>
  8. where
  9. I: ItemEntry,
  10. L: XLock,
  11. M: Into<XMark>,
  12. {
  13. cursor: Cursor<'a, I, L, M>,
  14. start: u64,
  15. end: u64,
  16. }
  17. impl<'a, I: ItemEntry, L: XLock, M: Into<XMark>> Range<'a, I, L, M> {
  18. pub(super) fn new(cursor: Cursor<'a, I, L, M>, start: u64, end: u64) -> Self {
  19. Range { cursor, start, end }
  20. }
  21. }
  22. impl<'a, I: ItemEntry, L: XLock, M: Into<XMark>> core::iter::Iterator for Range<'a, I, L, M> {
  23. type Item = (u64, ItemRef<'a, I>);
  24. fn next(&mut self) -> Option<Self::Item> {
  25. loop {
  26. if self.cursor.index() >= self.end {
  27. return None;
  28. }
  29. let item = self.cursor.load();
  30. if item.is_none() {
  31. self.cursor.next();
  32. continue;
  33. }
  34. let res = item.map(|item| (self.cursor.index(), item));
  35. self.cursor.next();
  36. return res;
  37. }
  38. }
  39. }