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