From 52cc9a023c66f41eae268582ad66b53869af7183 Mon Sep 17 00:00:00 2001 From: Ron Kuris Date: Tue, 28 Nov 2023 12:51:12 -0800 Subject: [PATCH] Expose NibblesIterator::is_empty (#389) --- firewood/src/nibbles.rs | 37 ++++++++++++++++++++++++++++++++++++- firewood/src/proof.rs | 4 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/firewood/src/nibbles.rs b/firewood/src/nibbles.rs index 8de0865eb..0f6152b06 100644 --- a/firewood/src/nibbles.rs +++ b/firewood/src/nibbles.rs @@ -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 } } @@ -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))); + } } diff --git a/firewood/src/proof.rs b/firewood/src/proof.rs index fa50404a6..b5b571c69 100644 --- a/firewood/src/proof.rs +++ b/firewood/src/proof.rs @@ -126,7 +126,7 @@ impl + Send> Proof { 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), .. @@ -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