proxmox-async: split blocking.rs into separate files

This commit is contained in:
Dietmar Maurer 2021-11-20 15:58:04 +01:00
parent 4413002f22
commit fa2032c7aa
3 changed files with 29 additions and 17 deletions

View File

@ -0,0 +1,8 @@
//! Async wrappers for blocking I/O (adding `block_in_place` around
//! channels/readers)
mod std_channel_stream;
pub use std_channel_stream::StdChannelStream;
mod wrapped_reader_stream;
pub use wrapped_reader_stream::WrappedReaderStream;

View File

@ -0,0 +1,21 @@
use std::pin::Pin;
use std::task::{Context, Poll};
use std::sync::mpsc::Receiver;
use futures::stream::Stream;
use crate::runtime::block_in_place;
/// 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
}
}
}

View File

@ -1,9 +1,6 @@
//! Async wrappers for blocking I/O (adding `block_in_place` around channels/readers)
use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::sync::mpsc::Receiver;
use futures::stream::Stream;
@ -43,20 +40,6 @@ 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;