`Mix` implements ExactSizeIterator but `len()` panics

Open
#947 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust

Research direction

Start with src/source/mix.rs and inspect how Mix implements ExactSizeIterator and size_hint(), then read the uniform format converters in src/source/uniform.rs. The report identifies two possible fixes but asks maintainers which they prefer, so first seek a decision on the contract before estimating completion; done means len() no longer panics for the reported inputs and the chosen behavior is covered.

Written by the indexing model from the issue text.

Description

Mix implements ExactSizeIterator, but its size_hint() can't always give an exact upper bound, so the default len() panics. Two same-format buffers are enough:

use rodio::{buffer::SamplesBuffer, nz, Source};

let mix = SamplesBuffer::new(nz!(1), nz!(1), [0.25])
    .mix(SamplesBuffer::new(nz!(1), nz!(1), [0.25, 0.5]));
assert_eq!(mix.clone().count(), 2);
let _ = mix.len(); // panics: upper bound of size_hint() is None

The inputs go through the uniform format converters (src/source/uniform.rs), whose upper bound is None here, and ExactSizeIterator::len asserts upper == Some(lower). On master (685e057) this panics in both debug and release.

Two ways I can see to fix it, and I'd like to know which you prefer before opening a PR:

  1. Remove the ExactSizeIterator impl for Mix (src/source/mix.rs). Simple, but it is a breaking change for anyone calling len().
  2. Keep the impl and make size_hint() exact when it can be: both inputs report exact lengths and need no resampling (same rate), so the output length is just the longer input converted to the shared channel count. That keeps len() working for the common case, but the impl would still panic for inputs that need resampling, so it may only move the problem.

I lean towards 1, since an ExactSizeIterator that panics for some inputs breaks its contract. Noticed while reviewing the stream adapters around #915.

Dominant language
Rust
Stars
2.5k
Forks
338
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RustAudio/rodio

All issues in RustAudio/rodio

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.