index: consistently use IntoIterator in RevWalk builder API

Since the return type is no longer "impl Iterator<..>", there isn't lifetime
issue anymore.
This commit is contained in:
Yuya Nishihara 2024-03-08 21:19:41 +09:00
parent 2615fed5be
commit f51c5d7e57
2 changed files with 17 additions and 9 deletions

View file

@ -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<Item = IndexPosition>,
) -> 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<Item = IndexPosition>,
) -> 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();

View file

@ -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| {