Skip to content

Mix implements ExactSizeIterator but len() panics #947

Backlog

Description

@kapok-4o

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions