diff --git a/library/core/src/stream/mod.rs b/library/core/src/stream/mod.rs index 58dc8e1e5e606..2e6e3be0afa6f 100644 --- a/library/core/src/stream/mod.rs +++ b/library/core/src/stream/mod.rs @@ -123,7 +123,11 @@ //! ``` mod from_iter; +mod pending; mod stream; pub use from_iter::{from_iter, FromIter}; pub use stream::Stream; + +#[unstable(feature = "stream_pending", issue = "91683")] +pub use pending::{pending, Pending}; diff --git a/library/core/src/stream/pending.rs b/library/core/src/stream/pending.rs new file mode 100644 index 0000000000000..89de07b88b00c --- /dev/null +++ b/library/core/src/stream/pending.rs @@ -0,0 +1,53 @@ +use core::fmt; +use core::marker::PhantomData; +use core::pin::Pin; +use core::stream::Stream; +use core::task::{Context, Poll}; + +/// Creates a stream that never returns any elements. +/// +/// The returned stream will always return `Pending` when polled. +#[unstable(feature = "stream_pending", issue = "91683")] +pub fn pending() -> Pending { + Pending { _t: PhantomData } +} + +/// A stream that never returns any elements. +/// +/// This stream is created by the [`pending`] function. See its +/// documentation for more. +#[must_use = "streams do nothing unless polled"] +#[unstable(feature = "stream_pending", issue = "91683")] +pub struct Pending { + _t: PhantomData, +} + +#[unstable(feature = "stream_pending", issue = "91683")] +impl Unpin for Pending {} + +#[unstable(feature = "stream_pending", issue = "91683")] +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)) + } +} + +#[unstable(feature = "stream_pending", issue = "91683")] +impl fmt::Debug for Pending { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Pending").finish() + } +} + +#[unstable(feature = "stream_pending", issue = "91683")] +impl Clone for Pending { + fn clone(&self) -> Self { + pending() + } +}