diff --git a/src/audio_unit/mod.rs b/src/audio_unit/mod.rs index d9b5b7e91..bf89a3dbf 100644 --- a/src/audio_unit/mod.rs +++ b/src/audio_unit/mod.rs @@ -89,7 +89,7 @@ struct InputCallback { macro_rules! try_os_status { - ($expr:expr) => (try!(Error::from_os_status($expr))) + ($expr:expr) => (Error::from_os_status($expr)?) } @@ -156,10 +156,11 @@ impl AudioUnit { } // Create an instance of the default audio unit using the component. - let mut instance: sys::AudioUnit = mem::uninitialized(); + let mut instance_uninit = mem::MaybeUninit::::uninit(); try_os_status!( - sys::AudioComponentInstanceNew(component, &mut instance as *mut sys::AudioUnit) + sys::AudioComponentInstanceNew(component, instance_uninit.as_mut_ptr() as *mut sys::AudioUnit) ); + let instance: sys::AudioUnit = instance_uninit.assume_init(); // Initialise the audio unit! try_os_status!(sys::AudioUnitInitialize(instance)); @@ -290,7 +291,7 @@ impl AudioUnit { /// Return the current Stream Format for the AudioUnit. pub fn stream_format(&self, scope: Scope) -> Result { let id = sys::kAudioUnitProperty_StreamFormat; - let asbd = try!(self.get_property(id, scope, Element::Output)); + let asbd = self.get_property(id, scope, Element::Output)?; StreamFormat::from_asbd(asbd) } @@ -391,12 +392,13 @@ pub fn get_property( let elem = elem as c_uint; let mut size = ::std::mem::size_of::() as u32; unsafe { - let mut data: T = ::std::mem::uninitialized(); - let data_ptr = &mut data as *mut _ as *mut c_void; + let mut data_uninit = ::std::mem::MaybeUninit::::uninit(); + let data_ptr = data_uninit.as_mut_ptr() as *mut _ as *mut c_void; let size_ptr = &mut size as *mut _; try_os_status!( sys::AudioUnitGetProperty(au, id, scope, elem, data_ptr, size_ptr) ); + let data: T = data_uninit.assume_init(); Ok(data) } } diff --git a/src/audio_unit/render_callback.rs b/src/audio_unit/render_callback.rs index 4dcbbdc13..45904503a 100644 --- a/src/audio_unit/render_callback.rs +++ b/src/audio_unit/render_callback.rs @@ -14,7 +14,7 @@ pub use self::data::Data; /// /// This allows the user to provide a custom, more rust-esque callback function type that takes /// greater advantage of rust's type safety. -pub type InputProcFn = FnMut(*mut sys::AudioUnitRenderActionFlags, +pub type InputProcFn = dyn FnMut(*mut sys::AudioUnitRenderActionFlags, *const sys::AudioTimeStamp, sys::UInt32, sys::UInt32, @@ -398,7 +398,7 @@ impl AudioUnit { // First, we'll retrieve the stream format so that we can ensure that the given callback // format matches the audio unit's format. let id = sys::kAudioUnitProperty_StreamFormat; - let asbd = try!(self.get_property(id, Scope::Input, Element::Output)); + let asbd = self.get_property(id, Scope::Input, Element::Output)?; let stream_format = super::StreamFormat::from_asbd(asbd)?; // If the stream format does not match, return an error indicating this. diff --git a/src/error.rs b/src/error.rs index 4a3509364..7117dff1d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,13 +45,7 @@ pub mod audio { impl ::std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - write!(f, "{:?}", self) - } - } - - impl ::std::error::Error for Error { - fn description(&self) -> &str { - match *self { + let description = match *self { Error::Unimplemented => "Unimplemented", Error::FileNotFound => "File not found", Error::FilePermission => "File permission", @@ -60,7 +54,8 @@ pub mod audio { Error::Param => "Param", Error::MemFull => "Memory full", Error::Unknown => "An unknown error occurred", - } + }; + write!(f, "{}", description) } } @@ -106,13 +101,7 @@ pub mod audio_codec { impl ::std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - write!(f, "{:?}", self) - } - } - - impl ::std::error::Error for Error { - fn description(&self) -> &str { - match *self { + let description = match *self { Error::Unspecified => "Unspecified", Error::UnknownProperty => "Unknown property", Error::BadPropertySize => "Bad property size", @@ -121,7 +110,8 @@ pub mod audio_codec { Error::State => "State", Error::NotEnoughBufferSpace => "Not enough buffer space", Error::Unknown => "Unknown error occurred", - } + }; + write!(f, "{}", description) } } @@ -161,13 +151,7 @@ pub mod audio_format { impl ::std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - write!(f, "{:?}", self) - } - } - - impl ::std::error::Error for Error { - fn description(&self) -> &str { - match *self { + let description = match *self { Error::Unspecified => "An unspecified error", Error::UnsupportedProperty => "The specified property is not supported", Error::BadPropertySize => "Bad property size", @@ -175,7 +159,8 @@ pub mod audio_format { Error::UnsupportedDataFormat => "The specified data format is not supported", Error::UnknownFormat => "The specified data format is not a known format", Error::Unknown => "Unknown error occurred", - } + }; + write!(f, "{}", description) } } } @@ -239,13 +224,7 @@ pub mod audio_unit { impl ::std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - write!(f, "{:?}", self) - } - } - - impl ::std::error::Error for Error { - fn description(&self) -> &str { - match *self { + let description = match *self { Error::InvalidProperty => "Invalid property", Error::InvalidParameter => "Invalid parameter", Error::InvalidElement => "Invalid element", @@ -264,7 +243,8 @@ pub mod audio_unit { Error::InvalidOfflineRender => "Invalid offline render", Error::Unauthorized => "Unauthorized", Error::Unknown => "Unknown error occurred", - } + }; + write!(f, "{}", description) } } @@ -338,25 +318,18 @@ impl Error { impl ::std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - write!(f, "{:?}", self) - } -} - -impl ::std::error::Error for Error { - fn description(&self) -> &str { match *self { - Error::Unspecified => "An unspecified error has occurred", - Error::NoMatchingDefaultAudioUnitFound => "No matching default audio unit found", + Error::Unspecified => write!(f, "An unspecified error has occurred"), + Error::NoMatchingDefaultAudioUnitFound => write!(f, "No matching default audio unit found"), Error::RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat => - "The given render callback buffer format does not match the `AudioUnit` `StreamFormat`", - Error::SystemSoundClientMessageTimedOut => "The system sound client message timed out", - Error::NoKnownSubtype => "The type has no known subtypes", - Error::Audio(ref err) => err.description(), - Error::AudioCodec(ref err) => err.description(), - Error::AudioFormat(ref err) => err.description(), - Error::AudioUnit(ref err) => err.description(), - Error::Unknown(_) => "An unknown error unknown to the coreaudio-rs API occurred", + write!(f, "The given render callback buffer format does not match the `AudioUnit` `StreamFormat`"), + Error::SystemSoundClientMessageTimedOut => write!(f, "The system sound client message timed out"), + Error::NoKnownSubtype => write!(f, "The type has no known subtypes"), + Error::Audio(ref err) => write!(f, "{}", err), + Error::AudioCodec(ref err) => write!(f, "{}", err), + Error::AudioFormat(ref err) => write!(f, "{}", err), + Error::AudioUnit(ref err) => write!(f, "{}", err), + Error::Unknown(_) => write!(f, "An unknown error unknown to the coreaudio-rs API occurred"), } } } -