From b891ff76b4b4325a4e3a7deb2eba40e2e2b4a74a Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Thu, 6 Oct 2022 16:08:59 -0500 Subject: [PATCH] Use proper error handling and not `unwrap()` --- src/lib.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 65a0e4e..8d4c229 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,6 +165,14 @@ error_from_format! { InitError } +#[cfg(feature = "audio")] +error_from_format! { + rodio::StreamError, + std::io::Error, + rodio::decoder::DecoderError, + rodio::PlayError +} + impl std::fmt::Display for CatboxError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) @@ -782,16 +790,18 @@ impl Game { #[cfg(feature = "audio")] pub fn play + std::marker::Send + 'static>(x: P, y: u64) -> thread::JoinHandle<()> { thread::spawn(move || { - let (_stream, stream_handle) = OutputStream::try_default().unwrap(); + let (_stream, stream_handle) = OutputStream::try_default()?; // Load a sound from a file, using a path relative to Cargo.toml - let file = BufReader::new(File::open(x).unwrap()); + let file = BufReader::new(File::open(p)?); // Decode that sound file into a source - let source = Decoder::new(file).unwrap(); + let source = Decoder::new(file)?; // Play the sound directly on the device - stream_handle.play_raw(source.convert_samples()).unwrap(); + stream_handle.play_raw(source.convert_samples())?; // The sound plays in a separate audio thread, // so we need to keep the main thread alive while it's playing. - std::thread::sleep(std::time::Duration::from_secs(y)); + std::thread::sleep(std::time::Duration::from_secs(time)); + + Ok(()) }) }