From 9ad225b3b5efd3c4704658598f9448afad99d143 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 20 Dec 2020 00:26:06 -0800 Subject: [PATCH] trees: make entries() function be the recursive one, since it's more common --- lib/src/store.rs | 8 ++++---- lib/src/tree.rs | 26 +++++++++++++------------- lib/src/trees.rs | 4 ++-- lib/tests/test_merge_trees.rs | 4 ++-- src/commands.rs | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/src/store.rs b/lib/src/store.rs index f06248dc7..292628c55 100644 --- a/lib/src/store.rs +++ b/lib/src/store.rs @@ -259,11 +259,11 @@ impl<'a> TreeEntry<'a> { } } -pub struct TreeEntriesIter<'a> { +pub struct TreeEntriesNonRecursiveIter<'a> { iter: std::collections::btree_map::Iter<'a, String, TreeValue>, } -impl<'a> Iterator for TreeEntriesIter<'a> { +impl<'a> Iterator for TreeEntriesNonRecursiveIter<'a> { type Item = TreeEntry<'a>; fn next(&mut self) -> Option { @@ -291,8 +291,8 @@ impl Tree { self.entries.is_empty() } - pub fn entries(&self) -> TreeEntriesIter { - TreeEntriesIter { + pub fn entries(&self) -> TreeEntriesNonRecursiveIter { + TreeEntriesNonRecursiveIter { iter: self.entries.iter(), } } diff --git a/lib/src/tree.rs b/lib/src/tree.rs index f17d0b827..78191a510 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -21,7 +21,7 @@ use crate::repo_path::{ DirRepoPath, DirRepoPathComponent, FileRepoPath, RepoPath, RepoPathComponent, RepoPathJoin, }; use crate::store; -use crate::store::{ConflictId, TreeEntriesIter, TreeEntry, TreeId, TreeValue}; +use crate::store::{ConflictId, TreeEntriesNonRecursiveIter, TreeEntry, TreeId, TreeValue}; use crate::store_wrapper::StoreWrapper; use crate::trees::{recursive_tree_diff, TreeValueDiff}; use std::pin::Pin; @@ -90,12 +90,12 @@ impl Tree { &self.data } - pub fn entries(&self) -> TreeEntriesIter { + pub fn entries_non_recursive(&self) -> TreeEntriesNonRecursiveIter { self.data.entries() } - pub fn entries_recursive(&self) -> TreeWalk { - TreeWalk::new(self.clone()) + pub fn entries(&self) -> TreeEntriesIter { + TreeEntriesIter::new(self.clone()) } pub fn entry(&self, basename: &N) -> Option @@ -194,7 +194,7 @@ impl Tree { pub fn conflicts(&self) -> Vec<(RepoPath, ConflictId)> { let mut conflicts = vec![]; - for (name, value) in self.entries_recursive() { + for (name, value) in self.entries() { if let TreeValue::Conflict(id) = value { conflicts.push((name.clone(), id.clone())); } @@ -203,22 +203,22 @@ impl Tree { } } -pub struct TreeWalk { - stack: Vec<(Pin>, TreeEntriesIter<'static>)>, +pub struct TreeEntriesIter { + stack: Vec<(Pin>, TreeEntriesNonRecursiveIter<'static>)>, } -impl TreeWalk { +impl TreeEntriesIter { fn new(tree: Tree) -> Self { let tree = Box::pin(tree); - let iter = tree.entries(); - let iter: TreeEntriesIter<'static> = unsafe { std::mem::transmute(iter) }; + let iter = tree.entries_non_recursive(); + let iter: TreeEntriesNonRecursiveIter<'static> = unsafe { std::mem::transmute(iter) }; Self { stack: vec![(tree, iter)], } } } -impl Iterator for TreeWalk { +impl Iterator for TreeEntriesIter { type Item = (RepoPath, TreeValue); fn next(&mut self) -> Option { @@ -235,8 +235,8 @@ impl Iterator for TreeWalk { let subtree = tree.known_sub_tree(&DirRepoPathComponent::from(entry.name()), id); let subtree = Box::pin(subtree); - let iter = subtree.entries(); - let subtree_iter: TreeEntriesIter<'static> = + let iter = subtree.entries_non_recursive(); + let subtree_iter: TreeEntriesNonRecursiveIter<'static> = unsafe { std::mem::transmute(iter) }; self.stack.push((subtree, subtree_iter)); } diff --git a/lib/src/trees.rs b/lib/src/trees.rs index 9267eab5d..d64ff7fa0 100644 --- a/lib/src/trees.rs +++ b/lib/src/trees.rs @@ -37,8 +37,8 @@ fn diff_entries<'a, E>( tree2: &'a Tree, callback: &mut impl FnMut(&'a str, TreeValueDiff<'a>) -> Result<(), E>, ) -> Result<(), E> { - let mut it1 = tree1.entries(); - let mut it2 = tree2.entries(); + let mut it1 = tree1.entries_non_recursive(); + let mut it2 = tree2.entries_non_recursive(); let mut entry1 = it1.next(); let mut entry2 = it2.next(); loop { diff --git a/lib/tests/test_merge_trees.rs b/lib/tests/test_merge_trees.rs index 66270d8aa..677729868 100644 --- a/lib/tests/test_merge_trees.rs +++ b/lib/tests/test_merge_trees.rs @@ -68,7 +68,7 @@ fn test_same_type(use_git: bool) { let side1_tree = write_tree(1); let side2_tree = write_tree(2); - // Created the merged tree + // Create the merged tree let merged_tree_id = trees::merge_trees(&side1_tree, &base_tree, &side2_tree).unwrap(); let merged_tree = store .get_tree(&DirRepoPath::root(), &merged_tree_id) @@ -76,7 +76,7 @@ fn test_same_type(use_git: bool) { // Check that we have exactly the paths we expect in the merged tree let names: Vec<&str> = merged_tree - .entries() + .entries_non_recursive() .map(|entry| entry.name().as_ref()) .collect(); assert_eq!( diff --git a/src/commands.rs b/src/commands.rs index fc360c68c..0e88f3708 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -565,7 +565,7 @@ fn cmd_files( let mut repo = get_repo(ui, &matches)?; let mut_repo = Arc::get_mut(&mut repo).unwrap(); let commit = resolve_revision_arg(ui, mut_repo, sub_matches)?; - for (name, _value) in commit.tree().entries_recursive() { + for (name, _value) in commit.tree().entries() { writeln!(ui, "{}", name.to_internal_string()); } Ok(())