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
1 changed files with 6 additions and 7 deletions

View File

@ -1,24 +1,23 @@
use std::io::Write; use std::io::Write;
use std::sync::mpsc::SyncSender; use std::sync::mpsc::SyncSender;
use std::string::ToString;
use anyhow::{Error};
/// Wrapper around SyncSender, which implements Write /// Wrapper around SyncSender, which implements Write
/// ///
/// Each write in translated into a send(Vec<u8>). /// 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 { impl <E: ToString> StdChannelWriter<E> {
pub fn new(sender: SyncSender<Result<Vec<u8>, Error>>) -> Self { pub fn new(sender: SyncSender<Result<Vec<u8>, E>>) -> Self {
Self(sender) Self(sender)
} }
} }
impl Write for StdChannelWriter { impl <E: ToString> Write for StdChannelWriter<E> {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> { fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
self.0 self.0
.send(Ok(buf.to_vec())) .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())) .and(Ok(buf.len()))
} }