From f8dd3d98169ef5efca51d37e5995b626cf324b6a Mon Sep 17 00:00:00 2001 From: Qifan Lu Date: Tue, 10 Dec 2019 18:18:15 -0800 Subject: [PATCH 1/4] Add stream::pending::{pending, Pending} --- src/stream/mod.rs | 2 ++ src/stream/pending.rs | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/stream/pending.rs diff --git a/src/stream/mod.rs b/src/stream/mod.rs index d8b96ec..0bfd4e8 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -325,6 +325,7 @@ cfg_unstable! { mod fused_stream; mod interval; mod into_stream; + mod pending; mod product; mod successors; mod sum; @@ -336,6 +337,7 @@ cfg_unstable! { pub use fused_stream::FusedStream; pub use interval::{interval, Interval}; pub use into_stream::IntoStream; + pub use pending::{pending, Pending}; pub use product::Product; pub use stream::Merge; pub use successors::{successors, Successors}; diff --git a/src/stream/pending.rs b/src/stream/pending.rs new file mode 100644 index 0000000..1fd0851 --- /dev/null +++ b/src/stream/pending.rs @@ -0,0 +1,49 @@ +use std::marker::PhantomData; +use std::pin::Pin; +use std::task::{Context, Poll}; + +use crate::stream::{Stream, DoubleEndedStream, ExactSizeStream, FusedStream}; + +/// A stream that never returns any items. +/// +/// This stream is created by the [`pending`] function. See its +/// documentation for more. +/// +/// [`pending`]: fn.pending.html +#[derive(Debug)] +pub struct Pending { + _marker: PhantomData +} + +/// Creates a stream that never returns any items. +/// +/// The returned stream will always return `Pending` when polled. +pub fn pending() -> Pending { + Pending { _marker: PhantomData } +} + +impl Stream for Pending { + type Item = T; + + fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Pending + } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } +} + +impl DoubleEndedStream for Pending { + fn poll_next_back(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Pending + } +} + +impl FusedStream for Pending {} + +impl ExactSizeStream for Pending { + fn len(&self) -> usize { + 0 + } +} From 879e14c6abf7d246871a7882075e8e8eb8c37d18 Mon Sep 17 00:00:00 2001 From: Qifan Lu Date: Tue, 10 Dec 2019 18:31:06 -0800 Subject: [PATCH 2/4] Remove size_hint from Stream impl --- src/stream/pending.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/stream/pending.rs b/src/stream/pending.rs index 1fd0851..943513e 100644 --- a/src/stream/pending.rs +++ b/src/stream/pending.rs @@ -28,10 +28,6 @@ impl Stream for Pending { fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { Poll::Pending } - - fn size_hint(&self) -> (usize, Option) { - (0, Some(0)) - } } impl DoubleEndedStream for Pending { From e9357c0307b8ea6bfaccccb0bb10ed4406960573 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 14 Jan 2020 09:49:34 +0900 Subject: [PATCH 3/4] style: Run `cargo fmt` --- src/stream/pending.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/stream/pending.rs b/src/stream/pending.rs index 943513e..5563d30 100644 --- a/src/stream/pending.rs +++ b/src/stream/pending.rs @@ -2,24 +2,26 @@ use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; -use crate::stream::{Stream, DoubleEndedStream, ExactSizeStream, FusedStream}; +use crate::stream::{DoubleEndedStream, ExactSizeStream, FusedStream, Stream}; /// A stream that never returns any items. -/// +/// /// This stream is created by the [`pending`] function. See its /// documentation for more. -/// +/// /// [`pending`]: fn.pending.html #[derive(Debug)] pub struct Pending { - _marker: PhantomData + _marker: PhantomData, } /// Creates a stream that never returns any items. -/// +/// /// The returned stream will always return `Pending` when polled. pub fn pending() -> Pending { - Pending { _marker: PhantomData } + Pending { + _marker: PhantomData, + } } impl Stream for Pending { From f53fcbb7060390757b91edd1bc195ecc804be730 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 14 Jan 2020 10:18:14 +0900 Subject: [PATCH 4/4] test,docs: Add stream::pending example code --- src/stream/pending.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/stream/pending.rs b/src/stream/pending.rs index 5563d30..922a540 100644 --- a/src/stream/pending.rs +++ b/src/stream/pending.rs @@ -18,6 +18,27 @@ pub struct Pending { /// Creates a stream that never returns any items. /// /// The returned stream will always return `Pending` when polled. +/// # Examples +/// +/// ``` +/// # async_std::task::block_on(async { +/// # +/// use std::time::Duration; +/// +/// use async_std::prelude::*; +/// use async_std::stream; +/// +/// let dur = Duration::from_millis(100); +/// let mut s = stream::pending::<()>().timeout(dur); +/// +/// let item = s.next().await; +/// +/// assert!(item.is_some()); +/// assert!(item.unwrap().is_err()); +/// +/// # +/// # }) +/// ``` pub fn pending() -> Pending { Pending { _marker: PhantomData,