From e6f751a24db51748a717eedd142d71ae6f4f3896 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 2 May 2021 14:33:38 -0700 Subject: [PATCH] revsets: generalize merge revset implementation to be a generic filter --- lib/src/revset.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 999ad08da..6612e3169 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -610,32 +610,31 @@ impl<'repo> Iterator for ChildrenRevsetIterator<'_, 'repo> { } } -// TODO: Generalize this to be take a predicate to apply to an &IndexEntry. -struct ParentCountRevset<'revset, 'repo: 'revset> { +struct FilterRevset<'revset, 'repo: 'revset> { candidates: Box + 'revset>, - parent_count_range: Range, + predicate: Box) -> bool>, } -impl<'repo> Revset<'repo> for ParentCountRevset<'_, 'repo> { +impl<'repo> Revset<'repo> for FilterRevset<'_, 'repo> { fn iter<'revset>(&'revset self) -> RevsetIterator<'revset, 'repo> { - RevsetIterator::new(Box::new(ParentCountRevsetIterator { + RevsetIterator::new(Box::new(FilterRevsetIterator { iter: self.candidates.iter(), - parent_count_range: self.parent_count_range.clone(), + predicate: self.predicate.as_ref(), })) } } -struct ParentCountRevsetIterator<'revset, 'repo> { +struct FilterRevsetIterator<'revset, 'repo> { iter: RevsetIterator<'revset, 'repo>, - parent_count_range: Range, + predicate: &'revset dyn Fn(&IndexEntry<'repo>) -> bool, } -impl<'revset, 'repo> Iterator for ParentCountRevsetIterator<'revset, 'repo> { +impl<'revset, 'repo> Iterator for FilterRevsetIterator<'revset, 'repo> { type Item = IndexEntry<'repo>; fn next(&mut self) -> Option { while let Some(next) = self.iter.next() { - if self.parent_count_range.contains(&next.num_parents()) { + if (self.predicate)(&next) { return Some(next); } } @@ -870,9 +869,10 @@ pub fn evaluate_expression<'revset, 'repo: 'revset>( parent_count_range, } => { let candidates = evaluate_expression(repo, candidates.as_ref())?; - Ok(Box::new(ParentCountRevset { + let parent_count_range = parent_count_range.clone(); + Ok(Box::new(FilterRevset { candidates, - parent_count_range: parent_count_range.clone(), + predicate: Box::new(move |entry| parent_count_range.contains(&entry.num_parents())), })) } RevsetExpression::PublicHeads => {