From c9ce80a82af9e26afbdaa9b3bf2dc4a95e2b3f6e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 29 Oct 2023 07:17:54 -0700 Subject: [PATCH] merged_tree: extract function for merged iterator of basenames in diff I'm going to reuse this for stream/async diffing. --- lib/src/merged_tree.rs | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/src/merged_tree.rs b/lib/src/merged_tree.rs index f86cd2986..3c4a7c93d 100644 --- a/lib/src/merged_tree.rs +++ b/lib/src/merged_tree.rs @@ -442,6 +442,32 @@ fn all_tree_basenames(trees: &Merge) -> impl Iterator( + tree1: &'a MergedTree, + tree2: &'a MergedTree, +) -> Box + 'a> { + fn merge_iters<'a>( + iter1: impl Iterator + 'a, + iter2: impl Iterator + 'a, + ) -> Box + 'a> { + Box::new(iter1.merge(iter2).dedup()) + } + match (&tree1, &tree2) { + (MergedTree::Legacy(before), MergedTree::Legacy(after)) => { + merge_iters(before.data().names(), after.data().names()) + } + (MergedTree::Merge(before), MergedTree::Legacy(after)) => { + merge_iters(all_tree_basenames(before), after.data().names()) + } + (MergedTree::Legacy(before), MergedTree::Merge(after)) => { + merge_iters(before.data().names(), all_tree_basenames(after)) + } + (MergedTree::Merge(before), MergedTree::Merge(after)) => { + merge_iters(all_tree_basenames(before), all_tree_basenames(after)) + } + } +} + fn merge_trees(merge: &Merge) -> Result, TreeMergeError> { if let Some(tree) = merge.resolve_trivial() { return Ok(Merge::resolved(tree.clone())); @@ -762,27 +788,7 @@ struct TreeEntryDiffIterator<'a> { impl<'a> TreeEntryDiffIterator<'a> { fn new(before: &'a MergedTree, after: &'a MergedTree) -> Self { - fn merge_iters<'a>( - iter1: impl Iterator + 'a, - iter2: impl Iterator + 'a, - ) -> Box + 'a> { - Box::new(iter1.merge(iter2).dedup()) - } - let basename_iter: Box + 'a> = - match (before, after) { - (MergedTree::Legacy(before), MergedTree::Legacy(after)) => { - merge_iters(before.data().names(), after.data().names()) - } - (MergedTree::Merge(before), MergedTree::Legacy(after)) => { - merge_iters(all_tree_basenames(before), after.data().names()) - } - (MergedTree::Legacy(before), MergedTree::Merge(after)) => { - merge_iters(before.data().names(), all_tree_basenames(after)) - } - (MergedTree::Merge(before), MergedTree::Merge(after)) => { - merge_iters(all_tree_basenames(before), all_tree_basenames(after)) - } - }; + let basename_iter = merged_tree_basenames(before, after); TreeEntryDiffIterator { before, after,