async_helpers/
maybe_stream.rsuse futures::stream::{FusedStream, Stream};
use futures::task::{Context, Poll};
use std::fmt::Debug;
use std::pin::Pin;
#[derive(Debug)]
pub struct MaybeStream<S: Stream>(Option<S>);
impl<S: Stream + Unpin> MaybeStream<S> {
pub fn take(&mut self) -> Option<S> {
self.0.take()
}
pub fn inner_mut(&mut self) -> Option<&mut S> {
self.0.as_mut()
}
pub fn is_some(&self) -> bool {
self.0.is_some()
}
pub fn set(&mut self, stream: S) {
self.0 = Some(stream)
}
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
Pin::new(self.0.as_mut().unwrap()).poll_next(cx)
}
}
impl<S: Stream> Default for MaybeStream<S> {
fn default() -> Self {
Self(None)
}
}
impl<S: Stream + Unpin> From<Option<S>> for MaybeStream<S> {
fn from(src: Option<S>) -> Self {
Self(src)
}
}
impl<S: Stream + Unpin> Stream for MaybeStream<S> {
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.0.is_none() {
return Poll::Pending;
}
self.get_mut().poll_next(cx)
}
}
impl<S: FusedStream + Stream + Unpin> FusedStream for MaybeStream<S> {
fn is_terminated(&self) -> bool {
if self.0.is_none() {
false
} else {
self.0.as_ref().unwrap().is_terminated()
}
}
}
impl<S: Stream + std::fmt::Display> std::fmt::Display for MaybeStream<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display_str = if let Some(st) = &self.0 { format!("{}", st) } else { "".to_string() };
write!(f, "MaybeStream({})", display_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
use fuchsia_async::TestExecutor;
use futures::stream::StreamExt;
struct CountStream {
count: usize,
}
impl CountStream {
fn new() -> CountStream {
CountStream { count: 0 }
}
}
impl Stream for CountStream {
type Item = usize;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.count += 1;
Poll::Ready(Some(self.count))
}
}
#[test]
fn maybestream() {
let mut exec = TestExecutor::new();
let mut s = MaybeStream::default();
let mut next_fut = s.next();
assert_eq!(Poll::Pending, exec.run_until_stalled(&mut next_fut));
next_fut = s.next();
assert_eq!(Poll::Pending, exec.run_until_stalled(&mut next_fut));
s.set(CountStream::new());
next_fut = s.next();
assert_eq!(Poll::Ready(Some(1)), exec.run_until_stalled(&mut next_fut));
next_fut = s.next();
assert_eq!(Poll::Ready(Some(2)), exec.run_until_stalled(&mut next_fut));
}
}