Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 133e30e

Browse files
Merge pull request #615 from lqf96/pending-stream
Add an implementation of pending stream
2 parents 76ed174 + f53fcbb commit 133e30e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

‎src/stream/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ cfg_unstable! {
325325
mod fused_stream;
326326
mod interval;
327327
mod into_stream;
328+
mod pending;
328329
mod product;
329330
mod successors;
330331
mod sum;
@@ -336,6 +337,7 @@ cfg_unstable! {
336337
pub use fused_stream::FusedStream;
337338
pub use interval::{interval, Interval};
338339
pub use into_stream::IntoStream;
340+
pub use pending::{pending, Pending};
339341
pub use product::Product;
340342
pub use stream::Merge;
341343
pub use successors::{successors, Successors};

‎src/stream/pending.rs‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /