mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-07 15:36:08 +00:00
proxmox-io: imported pbs-tools/src/sync/std_channel_writer.rs
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
parent
80df41a887
commit
41d3df8950
@ -15,6 +15,9 @@ pub use sparse_copy::{buffer_is_zero, sparse_copy, SparseCopyResult};
|
|||||||
#[cfg(feature = "tokio")]
|
#[cfg(feature = "tokio")]
|
||||||
pub use sparse_copy::sparse_copy_async;
|
pub use sparse_copy::sparse_copy_async;
|
||||||
|
|
||||||
|
mod std_channel_writer;
|
||||||
|
pub use std_channel_writer::StdChannelWriter;
|
||||||
|
|
||||||
mod byte_buffer;
|
mod byte_buffer;
|
||||||
pub use byte_buffer::ByteBuffer;
|
pub use byte_buffer::ByteBuffer;
|
||||||
|
|
||||||
|
27
proxmox-io/src/std_channel_writer.rs
Normal file
27
proxmox-io/src/std_channel_writer.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user