Try to wrap stream into SCC to factor the errored and completed checks

This commit is contained in:
Dominik Werder
2021-04-30 20:20:30 +02:00
parent 168e532974
commit a23400aaf8
5 changed files with 94 additions and 28 deletions

View File

@@ -10,6 +10,8 @@ use timeunits::*;
#[allow(unused_imports)]
use tracing::{debug, error, info, trace, warn};
pub mod streamext;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AggQuerySingleChannel {
pub channel_config: ChannelConfig,

72
netpod/src/streamext.rs Normal file
View File

@@ -0,0 +1,72 @@
use err::Error;
use futures_core::Stream;
use futures_util::StreamExt;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct SCC<S>
where
S: Stream,
{
inp: S,
errored: bool,
completed: bool,
}
impl<S> SCC<S>
where
S: Stream,
{
pub fn new(inp: S) -> Self {
Self {
inp,
errored: false,
completed: false,
}
}
}
impl<S, I> Stream for SCC<S>
where
S: Stream<Item = Result<I, Error>> + Unpin,
{
type Item = <S as Stream>::Item;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
use Poll::*;
if self.completed {
panic!("poll_next on completed");
} else if self.errored {
self.completed = true;
Ready(None)
} else {
match self.inp.poll_next_unpin(cx) {
Ready(Some(Ok(k))) => Ready(Some(Ok(k))),
Ready(Some(Err(e))) => {
self.errored = true;
Ready(Some(Err(e)))
}
Ready(None) => {
self.completed = true;
Ready(None)
}
Pending => Pending,
}
}
}
}
pub trait IntoSCC<S>
where
S: Stream,
{
fn into_scc(self) -> SCC<S>;
}
impl<S> IntoSCC<S> for S
where
S: Stream,
{
fn into_scc(self) -> SCC<S> {
SCC::new(self)
}
}