2016-12-31 12:24:18 +00:00
|
|
|
use super::{Open, Sink};
|
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::mem;
|
|
|
|
use std::slice;
|
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
pub struct StdoutSink(Box<dyn Write>);
|
2016-12-31 12:24:18 +00:00
|
|
|
|
|
|
|
impl Open for StdoutSink {
|
2017-01-18 18:41:22 +00:00
|
|
|
fn open(path: Option<String>) -> StdoutSink {
|
2016-12-31 12:24:18 +00:00
|
|
|
if let Some(path) = path {
|
|
|
|
let file = OpenOptions::new().write(true).open(path).unwrap();
|
|
|
|
StdoutSink(Box::new(file))
|
|
|
|
} else {
|
|
|
|
StdoutSink(Box::new(io::stdout()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for StdoutSink {
|
|
|
|
fn start(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
|
|
|
let data: &[u8] = unsafe {
|
2019-10-08 09:31:18 +00:00
|
|
|
slice::from_raw_parts(
|
|
|
|
data.as_ptr() as *const u8,
|
|
|
|
data.len() * mem::size_of::<i16>(),
|
|
|
|
)
|
2016-12-31 12:24:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.0.write_all(data)?;
|
|
|
|
self.0.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|