StdChannelWriter: avoid using anyhow::Error

Use a generic implementation to allow different error types.
This commit is contained in:
Dietmar Maurer 2021-11-25 11:14:56 +01:00
parent 92ef0b56d8
commit e2b12ce988

View File

@ -1,24 +1,23 @@
use std::io::Write;
use std::sync::mpsc::SyncSender;
use anyhow::{Error};
use std::string::ToString;
/// Wrapper around SyncSender, which implements Write
///
/// Each write in translated into a send(Vec<u8>).
pub struct StdChannelWriter(SyncSender<Result<Vec<u8>, Error>>);
pub struct StdChannelWriter<E>(SyncSender<Result<Vec<u8>, E>>);
impl StdChannelWriter {
pub fn new(sender: SyncSender<Result<Vec<u8>, Error>>) -> Self {
impl <E: ToString> StdChannelWriter<E> {
pub fn new(sender: SyncSender<Result<Vec<u8>, E>>) -> Self {
Self(sender)
}
}
impl Write for StdChannelWriter {
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(proxmox_sys::error::io_err_other)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))
.and(Ok(buf.len()))
}