Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core::stream::pending #91684

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add core::stream::pending
  • Loading branch information
ibraheemdev committed Dec 9, 2021
commit 8a0c3f845b96f584c78198dc95655b4df8a73fe9
4 changes: 4 additions & 0 deletions library/core/src/stream/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
53 changes: 53 additions & 0 deletions library/core/src/stream/pending.rs
Original file line number Diff line number Diff line change
@@ -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<T>() -> Pending<T> {
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> {
_t: PhantomData<T>,
}

#[unstable(feature = "stream_pending", issue = "91683")]
impl<T> Unpin for Pending<T> {}

#[unstable(feature = "stream_pending", issue = "91683")]
impl<T> Stream for Pending<T> {
type Item = T;

fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Pending
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(0))
}
}

#[unstable(feature = "stream_pending", issue = "91683")]
impl<T> fmt::Debug for Pending<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Pending").finish()
}
}

#[unstable(feature = "stream_pending", issue = "91683")]
impl<T> Clone for Pending<T> {
fn clone(&self) -> Self {
pending()
}
}