diff --git a/src/tools.rs b/src/tools.rs index ca7964b4..750364f8 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -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; diff --git a/src/tools/std_channel_writer.rs b/src/tools/std_channel_writer.rs new file mode 100644 index 00000000..8208b72b --- /dev/null +++ b/src/tools/std_channel_writer.rs @@ -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). +pub struct StdChannelWriter(SyncSender, Error>>); + +impl StdChannelWriter { + pub fn new(sender: SyncSender, Error>>) -> Self { + Self(sender) + } +} + +impl Write for StdChannelWriter { + fn write(&mut self, buf: &[u8]) -> Result { + 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(()) + } +}