alsa/
io.rs

1use crate::alsa;
2use super::error::*;
3use std::{slice, ptr, fmt};
4
5/// [snd_output_t](http://www.alsa-project.org/alsa-doc/alsa-lib/group___output.html) wrapper
6pub struct Output(*mut alsa::snd_output_t);
7
8unsafe impl Send for Output {}
9
10impl Drop for Output {
11    fn drop(&mut self) { unsafe { alsa::snd_output_close(self.0) }; }
12}
13
14impl Output {
15
16    pub fn buffer_open() -> Result<Output> {
17        let mut q = ptr::null_mut();
18        acheck!(snd_output_buffer_open(&mut q)).map(|_| Output(q))
19    }
20
21    pub fn buffer_string<T, F: FnOnce(&[u8]) -> T>(&self, f: F) -> T {
22        let b = unsafe {
23            let mut q = ptr::null_mut();
24            let s = alsa::snd_output_buffer_string(self.0, &mut q);
25            slice::from_raw_parts(q as *const u8, s as usize)
26        };
27        f(b)
28    }
29}
30
31impl fmt::Debug for Output {
32    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33        write!(f, "Output(")?;
34        fmt::Display::fmt(self, f)?;
35        write!(f, ")")
36        /* self.buffer_string(|b| f.write_str(try!(str::from_utf8(b).map_err(|_| fmt::Error)))) */
37    }
38}
39
40impl fmt::Display for Output {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        self.buffer_string(|b| {
43            let s = String::from_utf8_lossy(b);
44            f.write_str(&*s)
45        })
46    }
47}
48
49pub fn output_handle(o: &Output) -> *mut alsa::snd_output_t { o.0 }