Skip to content

Commit

Permalink
Use enumerate/find instead of position
Browse files Browse the repository at this point in the history
Per review comments
  • Loading branch information
rkuris committed Nov 14, 2023
1 parent 8b3648f commit 9803ae3
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,10 +1248,21 @@ impl<'a, S: shale::ShaleStore<node::Node> + Send + Sync> Stream for MerkleKeyVal
loop {
match last_node.inner() {
NodeType::Branch(branch) => {
let leftmost_position =
branch.children.iter().position(|&addr| addr.is_some());

let Some(leftmost_position) = leftmost_position else {
if let Some((leftmost_position, leftmost_address)) = branch
.children
.iter()
.enumerate()
.find(|&addr| addr.1.is_some())
{
let next = self
.merkle
.get_node(leftmost_address.unwrap())
.map_err(|e| api::Error::InternalError(e.into()))?;

parents.push((last_node, leftmost_position as u8));

last_node = next;
} else {
// we already exhausted the branch node. This happens with an empty trie
// ... or a corrupt one
return if parents.is_empty() {
Expand All @@ -1264,15 +1275,6 @@ impl<'a, S: shale::ShaleStore<node::Node> + Send + Sync> Stream for MerkleKeyVal
)))))
};
};

let next = self
.merkle
.get_node(branch.children[leftmost_position].unwrap())
.map_err(|e| api::Error::InternalError(e.into()))?;

parents.push((last_node, leftmost_position as u8));

last_node = next;
}
NodeType::Leaf(_) => break,
NodeType::Extension(_) => todo!(),
Expand Down

0 comments on commit 9803ae3

Please sign in to comment.