|
| 1 | +use std::marker::PhantomData; |
| 2 | +use std::pin::Pin; |
| 3 | +use std::task::{Context, Poll}; |
| 4 | + |
| 5 | +use crate::stream::{DoubleEndedStream, ExactSizeStream, FusedStream, Stream}; |
| 6 | + |
| 7 | +/// A stream that never returns any items. |
| 8 | +/// |
| 9 | +/// This stream is created by the [`pending`] function. See its |
| 10 | +/// documentation for more. |
| 11 | +/// |
| 12 | +/// [`pending`]: fn.pending.html |
| 13 | +#[derive(Debug)] |
| 14 | +pub struct Pending<T> { |
| 15 | + _marker: PhantomData<T>, |
| 16 | +} |
| 17 | + |
| 18 | +/// Creates a stream that never returns any items. |
| 19 | +/// |
| 20 | +/// The returned stream will always return `Pending` when polled. |
| 21 | +/// # Examples |
| 22 | +/// |
| 23 | +/// ``` |
| 24 | +/// # async_std::task::block_on(async { |
| 25 | +/// # |
| 26 | +/// use std::time::Duration; |
| 27 | +/// |
| 28 | +/// use async_std::prelude::*; |
| 29 | +/// use async_std::stream; |
| 30 | +/// |
| 31 | +/// let dur = Duration::from_millis(100); |
| 32 | +/// let mut s = stream::pending::<()>().timeout(dur); |
| 33 | +/// |
| 34 | +/// let item = s.next().await; |
| 35 | +/// |
| 36 | +/// assert!(item.is_some()); |
| 37 | +/// assert!(item.unwrap().is_err()); |
| 38 | +/// |
| 39 | +/// # |
| 40 | +/// # }) |
| 41 | +/// ``` |
| 42 | +pub fn pending<T>() -> Pending<T> { |
| 43 | + Pending { |
| 44 | + _marker: PhantomData, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<T> Stream for Pending<T> { |
| 49 | + type Item = T; |
| 50 | + |
| 51 | + fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>> { |
| 52 | + Poll::Pending |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<T> DoubleEndedStream for Pending<T> { |
| 57 | + fn poll_next_back(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>> { |
| 58 | + Poll::Pending |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<T> FusedStream for Pending<T> {} |
| 63 | + |
| 64 | +impl<T> ExactSizeStream for Pending<T> { |
| 65 | + fn len(&self) -> usize { |
| 66 | + 0 |
| 67 | + } |
| 68 | +} |
0 commit comments