Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rlp): len() changes as RlpIterator is consumed #766

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading