You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mix implements ExactSizeIterator but len() panics #947
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:
Remove the ExactSizeIterator impl for Mix (src/source/mix.rs). Simple, but it is a breaking change for anyone calling len().
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