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,3 +1,4 @@
use {BackendSpecificError, DevicesError};
use super::Device;
use super::alsa;
use super::check_errors;
@ -13,6 +14,28 @@ pub struct Devices {
next_str: *const *const u8,
}
impl Devices {
pub fn new() -> Result<Self, DevicesError> {
unsafe {
// TODO: check in which situation this can fail.
let card = -1; // -1 means all cards.
let iface = b"pcm\0"; // Interface identification.
let mut hints = mem::uninitialized(); // Array of device name hints.
let res = alsa::snd_device_name_hint(card, iface.as_ptr() as *const _, &mut hints);
if let Err(description) = check_errors(res) {
let err = BackendSpecificError { description };
return Err(err.into());
}
let hints = hints as *const *const u8;
let devices = Devices {
global_list: hints,
next_str: hints,
};
Ok(devices)
}
}
}
unsafe impl Send for Devices {
}
unsafe impl Sync for Devices {
@ -27,24 +50,6 @@ impl Drop for Devices {
}
}
impl Default for Devices {
fn default() -> Devices {
unsafe {
// TODO: check in which situation this can fail.
let card = -1; // -1 means all cards.
let iface = b"pcm\0"; // Interface identification.
let mut hints = mem::uninitialized(); // Array of device name hints.
let res = alsa::snd_device_name_hint(card, iface.as_ptr() as *const _, &mut hints);
check_errors(res).unwrap();
let hints = hints as *const *const u8;
Devices {
global_list: hints,
next_str: hints,
}
}
}
}
impl Iterator for Devices {
type Item = Device;