use err::Error; use futures_core::Stream; use futures_util::StreamExt; use std::pin::Pin; use std::task::{Context, Poll}; pub struct SCC where S: Stream, { inp: S, errored: bool, completed: bool, } impl SCC where S: Stream, { pub fn new(inp: S) -> Self { Self { inp, errored: false, completed: false, } } } impl Stream for SCC where S: Stream> + Unpin, { type Item = ::Item; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { 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 where S: Stream, { fn into_scc(self) -> SCC; } impl IntoSCC for S where S: Stream, { fn into_scc(self) -> SCC { SCC::new(self) } }