From f51c5d7e57525f8179569aee56079c4c7fa94b87 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Mar 2024 21:19:41 +0900 Subject: [PATCH] index: consistently use IntoIterator in RevWalk builder API Since the return type is no longer "impl Iterator<..>", there isn't lifetime issue anymore. --- lib/src/default_index/rev_walk.rs | 24 ++++++++++++++++-------- lib/src/default_index/revset_engine.rs | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/src/default_index/rev_walk.rs b/lib/src/default_index/rev_walk.rs index c9ecd91a4..af0da9a37 100644 --- a/lib/src/default_index/rev_walk.rs +++ b/lib/src/default_index/rev_walk.rs @@ -252,11 +252,17 @@ impl<'a> RevWalkBuilder<'a> { /// /// Use this if you are only interested in descendants of the given roots. /// The caller still needs to filter out unwanted entries. - pub fn ancestors_until_roots(self, root_positions: &[IndexPosition]) -> RevWalkAncestors<'a> { + pub fn ancestors_until_roots( + self, + root_positions: impl IntoIterator, + ) -> RevWalkAncestors<'a> { // We can also make it stop visiting based on the generation number. Maybe // it will perform better for unbalanced branchy history. // https://github.com/martinvonz/jj/pull/1492#discussion_r1160678325 - let min_pos = *root_positions.iter().min().unwrap_or(&IndexPosition::MAX); + let min_pos = root_positions + .into_iter() + .min() + .unwrap_or(IndexPosition::MAX); self.ancestors_with_min_pos(min_pos) } @@ -268,11 +274,13 @@ impl<'a> RevWalkBuilder<'a> { self, root_positions: impl IntoIterator, ) -> RevWalkDescendants<'a> { - // TODO: collect HashSet<_> directly - let root_positions = Vec::from_iter(root_positions); + let root_positions = HashSet::from_iter(root_positions); + let candidate_entries = self + .ancestors_until_roots(root_positions.iter().copied()) + .collect(); RevWalkDescendants { - candidate_entries: self.ancestors_until_roots(&root_positions).collect(), - root_positions: root_positions.into_iter().collect(), + candidate_entries, + root_positions, reachable_positions: HashSet::new(), } } @@ -291,7 +299,7 @@ impl<'a> RevWalkBuilder<'a> { ) -> RevWalkDescendantsGenerationRange<'a> { let index = self.index; let root_positions = Vec::from_iter(root_positions); - let entries = self.ancestors_until_roots(&root_positions); + let entries = self.ancestors_until_roots(root_positions.iter().copied()); let descendants_index = RevWalkDescendantsIndex::build(index, entries); let mut queue = RevWalkQueue::with_min_pos(Reverse(IndexPosition::MAX)); @@ -727,7 +735,7 @@ mod tests { let make_iter = |heads: &[CommitId], roots: &[CommitId]| { RevWalkBuilder::new(index) .wanted_heads(to_positions_vec(index, heads)) - .ancestors_until_roots(&to_positions_vec(index, roots)) + .ancestors_until_roots(to_positions_vec(index, roots)) }; let to_commit_id = |entry: IndexEntry| entry.commit_id(); diff --git a/lib/src/default_index/revset_engine.rs b/lib/src/default_index/revset_engine.rs index 8ed5bad8d..08e4a1d4c 100644 --- a/lib/src/default_index/revset_engine.rs +++ b/lib/src/default_index/revset_engine.rs @@ -815,7 +815,7 @@ impl<'index> EvaluationContext<'index> { Box::new( RevWalkBuilder::new(index) .wanted_heads(head_positions.iter().copied()) - .ancestors_until_roots(&root_positions), + .ancestors_until_roots(root_positions.iter().copied()), ) }); let predicate = as_pure_predicate_fn(move |_index, entry| {