Skip to content

Commit 7e66a4a

Browse files
cruesslerByron
authored andcommitted
Don't recurse, use loop instead
Return `Entry` instead of `EntryRef`. `EntryRef` needs the underlying data to be kept by `TreeRef`, but `lookup_entry` only temporarily loads sub-trees, so there is no data to return a reference to.
1 parent 0cf6d3a commit 7e66a4a

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

gix-object/src/tree/ref_iter.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,24 @@ impl<'a> TreeRef<'a> {
5959
odb: impl crate::Find + crate::FindExt,
6060
buffer: &'a mut Vec<u8>,
6161
path: I,
62-
) -> Result<Option<EntryRef<'a>>, Error>
62+
) -> Result<Option<tree::Entry>, Error>
6363
where
6464
I: IntoIterator<Item = P>,
6565
P: PartialEq<BStr>,
6666
{
6767
let mut path = path.into_iter().peekable();
68+
let mut entries = self.entries.clone();
6869

6970
while let Some(component) = path.next() {
70-
match self.entries.iter().find(|entry| component.eq(entry.filename)) {
71+
match entries.iter().find(|entry| component.eq(entry.filename)) {
7172
Some(entry) => {
7273
if path.peek().is_none() {
73-
return Ok(Some(*entry));
74+
return Ok(Some((*entry).into()));
7475
} else {
7576
let next_id = entry.oid.to_owned();
7677
let obj = odb.find_tree(&next_id, buffer)?;
7778

78-
return obj.lookup_entry(odb, buffer, path);
79+
entries = obj.entries;
7980
}
8081
}
8182
None => return Ok(None),
@@ -95,7 +96,7 @@ impl<'a> TreeRef<'a> {
9596
odb: impl crate::Find,
9697
buffer: &'a mut Vec<u8>,
9798
relative_path: impl AsRef<std::path::Path>,
98-
) -> Result<Option<EntryRef<'a>>, Error> {
99+
) -> Result<Option<tree::Entry>, Error> {
99100
use crate::bstr::ByteSlice;
100101
self.lookup_entry(
101102
odb,

gix-object/tests/object/tree/iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use gix_object::{
22
bstr::ByteSlice,
3-
tree::{self, EntryRef},
3+
tree::{self, Entry, EntryRef},
44
FindExt, TreeRefIter,
55
};
66
use pretty_assertions::assert_eq;
@@ -69,7 +69,7 @@ fn lookup_entry_toplevel() -> crate::Result {
6969
let mut buf = Vec::new();
7070
let entry = root_tree.lookup_entry_by_path(&odb, &mut buf, "bin").unwrap().unwrap();
7171

72-
assert!(matches!(entry, EntryRef { .. }));
72+
assert!(matches!(entry, Entry { .. }));
7373
assert_eq!(entry.filename, "bin");
7474

7575
Ok(())
@@ -89,7 +89,7 @@ fn lookup_entry_nested_path() -> crate::Result {
8989
.unwrap()
9090
.unwrap();
9191

92-
assert!(matches!(entry, EntryRef { .. }));
92+
assert!(matches!(entry, Entry { .. }));
9393
assert_eq!(entry.filename, "a");
9494

9595
Ok(())

0 commit comments

Comments
 (0)