Skip to content

Commit

Permalink
fix(rlp): len() changes as RlpIterator is consumed
Browse files Browse the repository at this point in the history
  Fixes #761
  • Loading branch information
achillelamb committed Jul 10, 2023
1 parent a5ef730 commit 0c7806f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rlp/src/rlpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl<'a, 'view> Iterator for RlpIterator<'a, 'view> {

impl<'a, 'view> ExactSizeIterator for RlpIterator<'a, 'view> {
fn len(&self) -> usize {
self.rlp.item_count().unwrap_or(0)
self.rlp.item_count().unwrap_or(0).saturating_sub(self.index)
}
}

Expand Down
6 changes: 6 additions & 0 deletions rlp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,20 @@ fn rlp_iter() {
let rlp = Rlp::new(&data);
let mut iter = rlp.iter();

assert_eq!(iter.len(), 2);

let cat = iter.next().unwrap();
assert!(cat.is_data());
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);

assert_eq!(iter.len(), 1);

let dog = iter.next().unwrap();
assert!(dog.is_data());
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);

assert_eq!(iter.len(), 0);

let none = iter.next();
assert!(none.is_none());

Expand Down

0 comments on commit 0c7806f

Please sign in to comment.