proxmox-backup/src/tools/std_channel_writer.rs
Wolfgang Bumiller f7d4e4b506 switch from failure to anyhow
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2020-04-17 18:43:30 +02:00

29 lines
704 B
Rust

use std::io::Write;
use std::sync::mpsc::SyncSender;
use anyhow::{Error};
/// Wrapper around SyncSender, which implements Write
///
/// Each write in translated into a send(Vec<u8>).
pub struct StdChannelWriter(SyncSender<Result<Vec<u8>, Error>>);
impl StdChannelWriter {
pub fn new(sender: SyncSender<Result<Vec<u8>, Error>>) -> Self {
Self(sender)
}
}
impl Write for StdChannelWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
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(())
}
}