src/tools/std_channel_writer.rs: new StdChannelWriter helper class

This commit is contained in:
Dietmar Maurer 2020-01-22 11:33:17 +01:00
parent 51f0ab1e8b
commit dcd033a53c
2 changed files with 32 additions and 0 deletions

View File

@ -28,6 +28,10 @@ pub mod ticket;
pub mod timer;
pub mod tty;
pub mod wrapped_reader_stream;
mod std_channel_writer;
pub use std_channel_writer::*;
pub mod xattr;
mod process_locker;

View File

@ -0,0 +1,28 @@
use std::io::Write;
use std::sync::mpsc::SyncSender;
use failure::*;
/// Wrapper around SyncSender, which implements Write
///
/// Each write in translated into a send(Vec<u8>).
pub struct StdChannelWriter(SyncSender<Result<Vec<u8>, Error>>);
impl StdChannelWriter {
pub fn new(sender: SyncSender<Result<Vec<u8>, Error>>) -> Self {
Self(sender)
}
}
impl Write for StdChannelWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
self.0
.send(Ok(buf.to_vec()))
.map_err(proxmox::sys::error::io_err_other)
.and(Ok(buf.len()))
}
fn flush(&mut self) -> Result<(), std::io::Error> {
Ok(())
}
}