From 507d0b5418208ff2f027c271729bc858c6dc42e8 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 14 Jun 2022 13:09:50 -0500 Subject: [PATCH] Replace some `unwrap` calls with `ok_or(Error::OperationFailed)`. --- src/backends/appkit.rs | 18 ++++++++++++------ src/backends/av_foundation.rs | 7 ++++--- src/lib.rs | 8 ++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/backends/appkit.rs b/src/backends/appkit.rs index 2918c09..cf1375a 100644 --- a/src/backends/appkit.rs +++ b/src/backends/appkit.rs @@ -12,12 +12,12 @@ use crate::{Backend, BackendId, Error, Features, UtteranceId, Voice}; pub(crate) struct AppKit(*mut Object, *mut Object); impl AppKit { - pub(crate) fn new() -> Self { + pub(crate) fn new() -> Result { info!("Initializing AppKit backend"); unsafe { let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new]; - let mut decl = - ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)).unwrap(); + let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)) + .ok_or(Error::OperationFailed)?; decl.add_ivar::("synth"); decl.add_ivar::("strings"); @@ -81,11 +81,17 @@ impl AppKit { let delegate_class = decl.register(); let delegate_obj: *mut Object = msg_send![delegate_class, new]; - delegate_obj.as_mut().unwrap().set_ivar("synth", obj); + delegate_obj + .as_mut() + .ok_or(Error::OperationFailed)? + .set_ivar("synth", obj); let strings: id = msg_send![class!(NSMutableArray), new]; - delegate_obj.as_mut().unwrap().set_ivar("strings", strings); + delegate_obj + .as_mut() + .ok_or(Error::OperationFailed)? + .set_ivar("strings", strings); let _: Object = msg_send![obj, setDelegate: delegate_obj]; - AppKit(obj, delegate_obj) + Ok(AppKit(obj, delegate_obj)) } } } diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index 6f0f1e0..a64d9b4 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -29,9 +29,10 @@ lazy_static! { } impl AvFoundation { - pub(crate) fn new() -> Self { + pub(crate) fn new() -> Result { info!("Initializing AVFoundation backend"); - let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)).unwrap(); + let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)) + .ok_or(Error::OperationFailed)?; decl.add_ivar::("backend_id"); extern "C" fn speech_synthesizer_did_start_speech_utterance( @@ -149,7 +150,7 @@ impl AvFoundation { } }; *backend_id += 1; - rv + Ok(rv) } } diff --git a/src/lib.rs b/src/lib.rs index 3da3bd6..76c7b94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -293,12 +293,12 @@ impl Tts { Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) } #[cfg(target_os = "macos")] - Backends::AppKit => Ok(Tts(Arc::new(RwLock::new( - Box::new(backends::AppKit::new()), - )))), + Backends::AppKit => Ok(Tts(Arc::new(RwLock::new(Box::new( + backends::AppKit::new()? + ))))), #[cfg(any(target_os = "macos", target_os = "ios"))] Backends::AvFoundation => Ok(Tts(Arc::new(RwLock::new(Box::new( - backends::AvFoundation::new(), + backends::AvFoundation::new()?, ))))), #[cfg(target_os = "android")] Backends::Android => {