mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
d26590afc5
- Fix deprecated Error::cause warnings and missing dyn - Reset max_width - Add rustfmt to Travis - Run rustfmt on full codebase with `cargo fmt --all` - Add rustfmt to Travis - Complete migration to edition 2018 - Replace try! shorthand - Use explicit `dyn Trait`
42 lines
949 B
Rust
42 lines
949 B
Rust
use super::{Open, Sink};
|
|
use std::fs::OpenOptions;
|
|
use std::io::{self, Write};
|
|
use std::mem;
|
|
use std::slice;
|
|
|
|
pub struct StdoutSink(Box<dyn Write>);
|
|
|
|
impl Open for StdoutSink {
|
|
fn open(path: Option<String>) -> StdoutSink {
|
|
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 {
|
|
slice::from_raw_parts(
|
|
data.as_ptr() as *const u8,
|
|
data.len() * mem::size_of::<i16>(),
|
|
)
|
|
};
|
|
|
|
self.0.write_all(data)?;
|
|
self.0.flush()?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|