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

オープン
#947 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
rust

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
2.5k
フォーク
338
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

RustAudio/rodio のほかの issue

RustAudio/rodio の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。