index: add RevWalk wrapper for eagerly evaluated set

This serves the same role as templater::Literal. I'm going to add basic
RevWalk adapters so that the revset evaluation tree can be constructed without
capturing the index. EagerRevWalk will help to write tests for these adapters.
This commit is contained in:
Yuya Nishihara 2024-03-10 13:23:43 +09:00
parent 7d43a5c2c0
commit 8f0b9a0e4a

View file

@ -16,7 +16,7 @@
use std::cmp::{max, Reverse};
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::iter::FusedIterator;
use std::iter::{Fuse, FusedIterator};
use std::ops::Range;
use smallvec::SmallVec;
@ -48,6 +48,29 @@ pub(super) trait RevWalk<I: ?Sized> {
}
}
/// Adapter that turns `Iterator` into `RevWalk` by dropping index argument.
///
/// As the name suggests, the source object is usually a slice or `Vec`.
#[derive(Clone, Debug)]
pub(super) struct EagerRevWalk<T> {
iter: Fuse<T>,
}
impl<T: Iterator> EagerRevWalk<T> {
#[allow(unused)] // TODO
pub fn new(iter: T) -> Self {
EagerRevWalk { iter: iter.fuse() }
}
}
impl<I: ?Sized, T: Iterator> RevWalk<I> for EagerRevWalk<T> {
type Item = T::Item;
fn next(&mut self, _index: &I) -> Option<Self::Item> {
self.iter.next()
}
}
/// Adapter that turns `RevWalk` into `Iterator` by attaching borrowed `index`.
#[derive(Clone, Debug)]
#[must_use]