src/bin/proxmox-backup-client.rs: use a std channel to write the catalog

This commit is contained in:
Dietmar Maurer
2020-01-22 12:49:08 +01:00
parent 02141b4d9b
commit f1d99e3f6a
4 changed files with 27 additions and 8 deletions

View File

@ -1,11 +1,14 @@
use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::sync::mpsc::Receiver;
use futures::stream::Stream;
use crate::tools::runtime::block_in_place;
/// Wrapper struct to convert a Reader into a Stream
pub struct WrappedReaderStream<R: Read + Unpin> {
reader: R,
buffer: Vec<u8>,
@ -39,6 +42,21 @@ impl<R: Read + Unpin> Stream for WrappedReaderStream<R> {
}
}
/// Wrapper struct to convert a channel Receiver into a Stream
pub struct StdChannelStream<T>(pub Receiver<T>);
impl<T> Stream for StdChannelStream<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
match block_in_place(|| self.0.recv()) {
Ok(data) => Poll::Ready(Some(data)),
Err(_) => Poll::Ready(None),// channel closed
}
}
}
#[cfg(test)]
mod test {
use std::io;