Add BackendSpecificError. Add new DevicesError.

See the documentation for both new errors for details.

The new `DevicesError` has been added to allow for returning errors when
enumerating devices. This has allowed to remove multiple potential
`panic!`s in each of the alsa, coreaudio and wasapi backends.
This commit is contained in:
mitchmindtree 2019-06-20 22:37:36 +02:00
parent cf84ab906f
commit 42fc702f53
8 changed files with 140 additions and 63 deletions

View file

@ -1,4 +1,4 @@
use SupportedFormat;
use {BackendSpecificError, DevicesError, SupportedFormat};
use std::mem;
use std::ptr::null;
use std::vec::IntoIter as VecIntoIter;
@ -64,20 +64,27 @@ unsafe fn audio_devices() -> Result<Vec<AudioDeviceID>, OSStatus> {
pub struct Devices(VecIntoIter<AudioDeviceID>);
impl Devices {
pub fn new() -> Result<Self, DevicesError> {
let devices = unsafe {
match audio_devices() {
Ok(devices) => devices,
Err(os_status) => {
let description = format!("{}", os_status);
let err = BackendSpecificError { description };
return Err(err.into());
}
}
};
Ok(Devices(devices.into_iter()))
}
}
unsafe impl Send for Devices {
}
unsafe impl Sync for Devices {
}
impl Default for Devices {
fn default() -> Self {
let devices = unsafe {
audio_devices().expect("failed to get audio output devices")
};
Devices(devices.into_iter())
}
}
impl Iterator for Devices {
type Item = Device;
fn next(&mut self) -> Option<Device> {