proxmox/proxmox-async/src/blocking/std_channel_stream.rs
Wolfgang Bumiller de891b1f76 proxmox_async: rustfmt
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2022-02-02 12:55:01 +01:00

22 lines
596 B
Rust

use std::pin::Pin;
use std::sync::mpsc::Receiver;
use std::task::{Context, Poll};
use futures::stream::Stream;
use crate::runtime::block_in_place;
/// Wrapper struct to convert a sync 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
}
}
}