proxmox-io: imported pbs-tools/src/sync/std_channel_writer.rs

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-11-25 11:21:31 +01:00
parent 80df41a887
commit 41d3df8950
2 changed files with 30 additions and 0 deletions

View File

@ -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;

View File

@ -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<u8>).
pub struct StdChannelWriter<E>(SyncSender<Result<Vec<u8>, E>>);
impl <E: ToString> StdChannelWriter<E> {
pub fn new(sender: SyncSender<Result<Vec<u8>, E>>) -> Self {
Self(sender)
}
}
impl <E: ToString> Write for StdChannelWriter<E> {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
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(())
}
}