Skip to content

Fix iterator size_hint after next() or nth() - #152

Open
bardrop wants to merge 1 commit into
NULLx76:mainfrom
bardrop-oss:fix-iter-size-hint
Open

bardrop wants to merge 1 commit into
NULLx76:mainfrom
bardrop-oss:fix-iter-size-hint

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown

iter() and iter_mut() always report the full buffer length in size_hint, even after some items are taken. So for a buffer [1, 2, 3, 4], let mut it = rb.iter(); it.next(); it.len() gives 4, but only 3 items are left. This breaks the ExactSizeIterator contract.

The fix returns the number of items still left (len - index, saturating, since nth_back can move len below index).

Tested with a new iter_len_after_next test for all three buffer types (fails before, passes after). cargo test --all-features, cargo fmt --check and cargo clippy pass.

RingBufferIterator and RingBufferMutIterator returned the full length
from size_hint, even after next() or nth() moved the front index. So
ExactSizeIterator::len() was wrong once iteration had started.

Return len - index instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant