mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-03 11:49:06 +00:00
proxmox-async: split blocking.rs into separate files
This commit is contained in:
parent
4413002f22
commit
fa2032c7aa
8
proxmox-async/src/blocking/mod.rs
Normal file
8
proxmox-async/src/blocking/mod.rs
Normal 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;
|
21
proxmox-async/src/blocking/std_channel_stream.rs
Normal file
21
proxmox-async/src/blocking/std_channel_stream.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
Loading…
Reference in New Issue
Block a user