use std::pin::Pin;
pub trait IntoStreamer: futures_core::Stream {
fn into_streamer(self) -> Streamer<Self>
where
Self: Sized + Unpin,
{
Streamer { inner: self }
}
}
impl<S> IntoStreamer for S where S: futures_core::Stream {}
pub struct Streamer<S> {
inner: S,
}
impl<S> Streamer<S> {
pub async fn next(&mut self) -> Option<S::Item>
where
S: futures_core::Stream + Unpin,
{
std::future::poll_fn(|cx| Pin::new(&mut self.inner).poll_next(cx)).await
}
pub async fn try_next<T, E>(&mut self) -> Result<Option<T>, E>
where
S: futures_core::Stream<Item = Result<T, E>> + Unpin,
{
self.next().await.transpose()
}
}