From dcd033a53c9282b3cf6dc69fe7866a73b39dd88d Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 22 Jan 2020 11:33:17 +0100 Subject: [PATCH] src/tools/std_channel_writer.rs: new StdChannelWriter helper class --- src/tools.rs | 4 ++++ src/tools/std_channel_writer.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/tools/std_channel_writer.rs 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(()) + } +}