Skip to content

Commit

Permalink
Expose NibblesIterator::is_empty (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Nov 28, 2023
1 parent 72921d0 commit 52cc9a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
37 changes: 36 additions & 1 deletion firewood/src/nibbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'a, const LEADING_ZEROES: usize> Iterator for NibblesIterator<'a, LEADING_Z

impl<'a, const LEADING_ZEROES: usize> NibblesIterator<'a, LEADING_ZEROES> {
#[inline(always)]
fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.head == self.tail
}
}
Expand Down Expand Up @@ -215,4 +215,39 @@ mod test {

assert!(nib_iter.eq(expected));
}

#[test]
fn empty() {
let nib = Nibbles::<0>(&[]);
assert!(nib.is_empty());
let it = nib.into_iter();
assert!(it.is_empty());
assert_eq!(it.size_hint().0, 0);
}

#[test]
fn not_empty_because_of_leading_nibble() {
let nib = Nibbles::<1>(&[]);
assert!(!nib.is_empty());
let mut it = nib.into_iter();
assert!(!it.is_empty());
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(0));
assert!(it.is_empty());
assert_eq!(it.size_hint(), (0, Some(0)));
}
#[test]
fn not_empty_because_of_data() {
let nib = Nibbles::<0>(&[1]);
assert!(!nib.is_empty());
let mut it = nib.into_iter();
assert!(!it.is_empty());
assert_eq!(it.size_hint(), (2, Some(2)));
assert_eq!(it.next(), Some(0));
assert!(!it.is_empty());
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(1));
assert!(it.is_empty());
assert_eq!(it.size_hint(), (0, Some(0)));
}
}
4 changes: 2 additions & 2 deletions firewood/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<N: AsRef<[u8]> + Send> Proof<N> {

cur_hash = match sub_proof {
// Return when reaching the end of the key.
Some(p) if key_nibbles.size_hint().0 == 0 => break p.encoded,
Some(p) if key_nibbles.is_empty() => break p.encoded,
// The trie doesn't contain the key.
Some(SubProof {
hash: Some(hash), ..
Expand Down Expand Up @@ -542,7 +542,7 @@ fn locate_subproof(

Ok((sub_proof.into(), key_nibbles))
}
NodeType::Branch(_) if key_nibbles.size_hint().0 == 0 => Err(ProofError::NoSuchNode),
NodeType::Branch(_) if key_nibbles.is_empty() => Err(ProofError::NoSuchNode),
NodeType::Branch(n) => {
let index = key_nibbles.next().unwrap() as usize;
// consume items returning the item at index
Expand Down

0 comments on commit 52cc9a0

Please sign in to comment.