From 41d3df89505ef62e778fcb9d5447b3ac324cee51 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 25 Nov 2021 11:21:31 +0100 Subject: [PATCH] proxmox-io: imported pbs-tools/src/sync/std_channel_writer.rs Signed-off-by: Dietmar Maurer --- proxmox-io/src/lib.rs | 3 +++ proxmox-io/src/std_channel_writer.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 proxmox-io/src/std_channel_writer.rs diff --git a/proxmox-io/src/lib.rs b/proxmox-io/src/lib.rs index 7e3e1386..73de0510 100644 --- a/proxmox-io/src/lib.rs +++ b/proxmox-io/src/lib.rs @@ -15,6 +15,9 @@ pub use sparse_copy::{buffer_is_zero, sparse_copy, SparseCopyResult}; #[cfg(feature = "tokio")] pub use sparse_copy::sparse_copy_async; +mod std_channel_writer; +pub use std_channel_writer::StdChannelWriter; + mod byte_buffer; pub use byte_buffer::ByteBuffer; diff --git a/proxmox-io/src/std_channel_writer.rs b/proxmox-io/src/std_channel_writer.rs new file mode 100644 index 00000000..c5131a12 --- /dev/null +++ b/proxmox-io/src/std_channel_writer.rs @@ -0,0 +1,27 @@ +use std::io::Write; +use std::sync::mpsc::SyncSender; +use std::string::ToString; + +/// Wrapper around SyncSender, which implements Write +/// +/// Each write in translated into a send(Vec). +pub struct StdChannelWriter(SyncSender, E>>); + +impl StdChannelWriter { + pub fn new(sender: SyncSender, E>>) -> Self { + Self(sender) + } +} + +impl Write for StdChannelWriter { + fn write(&mut self, buf: &[u8]) -> Result { + self.0 + .send(Ok(buf.to_vec())) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string())) + .and(Ok(buf.len())) + } + + fn flush(&mut self) -> Result<(), std::io::Error> { + Ok(()) + } +}