From 8f0b9a0e4a4505e3f2892dd97aa0fc997318e7ba Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 10 Mar 2024 13:23:43 +0900 Subject: [PATCH] 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. --- lib/src/default_index/rev_walk.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/src/default_index/rev_walk.rs b/lib/src/default_index/rev_walk.rs index 9c291f919..36eb0ec0f 100644 --- a/lib/src/default_index/rev_walk.rs +++ b/lib/src/default_index/rev_walk.rs @@ -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 { } } +/// 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 { + iter: Fuse, +} + +impl EagerRevWalk { + #[allow(unused)] // TODO + pub fn new(iter: T) -> Self { + EagerRevWalk { iter: iter.fuse() } + } +} + +impl RevWalk for EagerRevWalk { + type Item = T::Item; + + fn next(&mut self, _index: &I) -> Option { + self.iter.next() + } +} + /// Adapter that turns `RevWalk` into `Iterator` by attaching borrowed `index`. #[derive(Clone, Debug)] #[must_use]