output stream dropped when ignoring stream variable
Not sure if this should be more obvious than it was for me, but I burned some time debugging a situation where ignoring the first return value of try_default below caused the stream to be dropped and the program to silently exit without playing anything.
let (_stream, stream_handle) = rodio::OutputStream::try_default()?;
Replacing _stream with _ causes programs using the stream_handle to fail, as I assume the stream was optimized away or closed immediately after creation. It was surprising to me that the lifetime of the stream object wasn't connected to the handle's lifetime in a way to avoid this.
If there's nothing to be improved, hopefully this at least helps someone debug a similar situation, as I almost abandoned using the library, thinking it had some fundamental bug and only discovered this fix by mistake.
Just had this bug myself. The stream is indeed being dropped. I assume this is due to the weak Arcs that OutputStreamHandle is using. It's very confusing since the error says NoDevice when doing something like creating a sink or using play_raw instead of like StreamDropped (even Copilot suggests this lol).
Full (bad) error output for future Googler's:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NoDevice'
For people getting this error when compiling for WASM, ensure (If you're not enabling all features) that you've got the wasm-bindgen feature enabled for rodio. That was my problem, anyway
Replacing
_streamwith_causes programs using the stream_handle to fail, as I assume the stream was optimized away or closed immediately after creation.
Underscore is a pattern that matches everything and does not bind to a value. You are effectively dropping (and thus closing) the output-stream at the end of the line.
Here are some docs regarding _ (its not really explained in the book I think).
- https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html?highlight=underscore#ignoring-an-entire-value-with-_
- https://doc.rust-lang.org/stable/reference/patterns.html?highlight=single%20underscore#wildcard-pattern
- https://doc.rust-lang.org/stable/reference/identifiers.html?highlight=underscore#identifiers
Feel free to submit a PR to improve the docs. A warning about dropping Self would be useful here.