range.rs 1.1 KB

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