]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-tools/src/sync/std_channel_writer.rs
d2f6444ebce59f5d0a0c41d55efa3bfaf3a1d5b7
[proxmox-backup.git] / pbs-tools / src / sync / std_channel_writer.rs
1 use std::io::Write;
2 use std::sync::mpsc::SyncSender;
3
4 use anyhow::{Error};
5
6 /// Wrapper around SyncSender, which implements Write
7 ///
8 /// Each write in translated into a send(Vec<u8>).
9 pub struct StdChannelWriter(SyncSender<Result<Vec<u8>, Error>>);
10
11 impl StdChannelWriter {
12 pub fn new(sender: SyncSender<Result<Vec<u8>, Error>>) -> Self {
13 Self(sender)
14 }
15 }
16
17 impl Write for StdChannelWriter {
18 fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
19 self.0
20 .send(Ok(buf.to_vec()))
21 .map_err(proxmox::sys::error::io_err_other)
22 .and(Ok(buf.len()))
23 }
24
25 fn flush(&mut self) -> Result<(), std::io::Error> {
26 Ok(())
27 }
28 }