Skip to content

Commit 1165de0

Browse files
committed
change!: Use new gix-object::Find trait
1 parent 806ea47 commit 1165de0

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

gix-index/src/extension/tree/verify.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cmp::Ordering;
22

33
use bstr::{BString, ByteSlice};
4+
use gix_object::FindExt;
45

56
use crate::extension::Tree;
67

@@ -14,8 +15,8 @@ pub enum Error {
1415
entry_id: gix_hash::ObjectId,
1516
name: BString,
1617
},
17-
#[error("The tree with id {oid} wasn't found in the object database")]
18-
TreeNodeNotFound { oid: gix_hash::ObjectId },
18+
#[error(transparent)]
19+
TreeNodeNotFound(#[from] gix_object::find::existing_iter::Error),
1920
#[error("The tree with id {oid} should have {expected_childcount} children, but its cached representation had {actual_childcount} of them")]
2021
TreeNodeChildcountMismatch {
2122
oid: gix_hash::ObjectId,
@@ -39,20 +40,14 @@ pub enum Error {
3940
}
4041

4142
impl Tree {
42-
///
43-
pub fn verify<F>(&self, use_find: bool, mut find: F) -> Result<(), Error>
44-
where
45-
F: for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Option<gix_object::TreeRefIter<'a>>,
46-
{
47-
fn verify_recursive<F>(
43+
/// Validate the correctness of this instance. If `use_objects` is true, then `objects` will be used to access all objects.
44+
pub fn verify(&self, use_objects: bool, objects: impl gix_object::Find) -> Result<(), Error> {
45+
fn verify_recursive(
4846
parent_id: gix_hash::ObjectId,
4947
children: &[Tree],
50-
mut find_buf: Option<&mut Vec<u8>>,
51-
find: &mut F,
52-
) -> Result<Option<u32>, Error>
53-
where
54-
F: for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Option<gix_object::TreeRefIter<'a>>,
55-
{
48+
mut object_buf: Option<&mut Vec<u8>>,
49+
objects: &impl gix_object::Find,
50+
) -> Result<Option<u32>, Error> {
5651
if children.is_empty() {
5752
return Ok(None);
5853
}
@@ -71,8 +66,8 @@ impl Tree {
7166
}
7267
prev = Some(child);
7368
}
74-
if let Some(buf) = find_buf.as_mut() {
75-
let tree_entries = find(&parent_id, buf).ok_or(Error::TreeNodeNotFound { oid: parent_id })?;
69+
if let Some(buf) = object_buf.as_mut() {
70+
let tree_entries = objects.find_tree_iter(&parent_id, buf)?;
7671
let mut num_entries = 0;
7772
for entry in tree_entries
7873
.filter_map(Result::ok)
@@ -99,7 +94,8 @@ impl Tree {
9994
for child in children {
10095
// This is actually needed here as it's a mut ref, which isn't copy. We do a re-borrow here.
10196
#[allow(clippy::needless_option_as_deref)]
102-
let actual_num_entries = verify_recursive(child.id, &child.children, find_buf.as_deref_mut(), find)?;
97+
let actual_num_entries =
98+
verify_recursive(child.id, &child.children, object_buf.as_deref_mut(), objects)?;
10399
if let Some((actual, num_entries)) = actual_num_entries.zip(child.num_entries) {
104100
if actual > num_entries {
105101
return Err(Error::EntriesCount {
@@ -120,7 +116,7 @@ impl Tree {
120116
}
121117

122118
let mut buf = Vec::new();
123-
let declared_entries = verify_recursive(self.id, &self.children, use_find.then_some(&mut buf), &mut find)?;
119+
let declared_entries = verify_recursive(self.id, &self.children, use_objects.then_some(&mut buf), &objects)?;
124120
if let Some((actual, num_entries)) = declared_entries.zip(self.num_entries) {
125121
if actual > num_entries {
126122
return Err(Error::EntriesCount {

gix-index/src/init.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod from_tree {
44
use bstr::{BStr, BString, ByteSlice, ByteVec};
55
use gix_object::{
66
tree::{self, EntryMode},
7-
TreeRefIter,
7+
FindExt,
88
};
99
use gix_traverse::tree::{breadthfirst, visit::Action, Visit};
1010

@@ -32,19 +32,18 @@ mod from_tree {
3232
}
3333
}
3434
/// Create an index [`State`] by traversing `tree` recursively, accessing sub-trees
35-
/// with `find`.
35+
/// with `objects`.
3636
///
3737
/// **No extension data is currently produced**.
38-
pub fn from_tree<Find>(tree: &gix_hash::oid, mut find: Find) -> Result<Self, breadthfirst::Error>
38+
pub fn from_tree<Find>(tree: &gix_hash::oid, objects: Find) -> Result<Self, breadthfirst::Error>
3939
where
40-
Find: for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Option<TreeRefIter<'a>>,
40+
Find: gix_object::Find,
4141
{
4242
let _span = gix_features::trace::coarse!("gix_index::State::from_tree()");
4343
let mut buf = Vec::new();
44-
let root = find(tree, &mut buf).ok_or(breadthfirst::Error::Find { oid: tree.into() })?;
45-
44+
let root = objects.find_tree_iter(tree, &mut buf)?;
4645
let mut delegate = CollectEntries::new();
47-
breadthfirst(root, breadthfirst::State::default(), &mut find, &mut delegate)?;
46+
breadthfirst(root, breadthfirst::State::default(), &objects, &mut delegate)?;
4847

4948
let CollectEntries {
5049
mut entries,

gix-index/src/verify.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ impl State {
6161
Ok(())
6262
}
6363

64-
/// Note: `find` cannot be `Option<F>` as we can't call it with a closure then due to the indirection through `Some`.
65-
pub fn verify_extensions<F>(&self, use_find: bool, find: F) -> Result<(), extensions::Error>
66-
where
67-
F: for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Option<gix_object::TreeRefIter<'a>>,
68-
{
69-
self.tree().map(|t| t.verify(use_find, find)).transpose()?;
64+
/// Note: `objects` cannot be `Option<F>` as we can't call it with a closure then due to the indirection through `Some`.
65+
pub fn verify_extensions(&self, use_find: bool, objects: impl gix_object::Find) -> Result<(), extensions::Error> {
66+
self.tree().map(|t| t.verify(use_find, objects)).transpose()?;
7067
// TODO: verify links by running the whole set of tests on the index
7168
// - do that once we load it as well, or maybe that's lazy loaded? Too many questions for now.
7269
Ok(())

0 commit comments

Comments
 (0)