1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use alsa;
use super::error::*;
use std::{slice, ptr, fmt};

/// [snd_output_t](http://www.alsa-project.org/alsa-doc/alsa-lib/group___output.html) wrapper
pub struct Output(*mut alsa::snd_output_t);

unsafe impl Send for Output {}

impl Drop for Output {
    fn drop(&mut self) { unsafe { alsa::snd_output_close(self.0) }; }
}

impl Output {

    pub fn buffer_open() -> Result<Output> {
        let mut q = ptr::null_mut();
        acheck!(snd_output_buffer_open(&mut q)).map(|_| Output(q))
    }

    pub fn buffer_string<T, F: FnOnce(&[u8]) -> T>(&self, f: F) -> T {
        let b = unsafe {
            let mut q = ptr::null_mut();
            let s = alsa::snd_output_buffer_string(self.0, &mut q);
            slice::from_raw_parts(q as *const u8, s as usize) 
        };
        f(b)
    }
}

impl fmt::Debug for Output {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "Output("));
        try!((&self as &fmt::Display).fmt(f));
        write!(f, ")")
        /* self.buffer_string(|b| f.write_str(try!(str::from_utf8(b).map_err(|_| fmt::Error)))) */
    }
}

impl fmt::Display for Output {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.buffer_string(|b| {
            let s = String::from_utf8_lossy(b);
            f.write_str(&*s)
        })
    }
}

pub fn output_handle(o: &Output) -> *mut alsa::snd_output_t { o.0 }