Skip to content

Commit fd940b8

Browse files
authored
Merge pull request #348 from k-nasa/add_stream_timeout
Add stream timeout
2 parents 1baee98 + feeb3c1 commit fd940b8

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/stream/stream/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ use std::marker::PhantomData;
9393

9494
cfg_unstable! {
9595
use std::pin::Pin;
96+
use std::time::Duration;
9697

9798
use crate::future::Future;
9899
use crate::stream::FromStream;
99100

100101
pub use merge::Merge;
102+
pub use timeout::{TimeoutError, Timeout};
101103

102104
mod merge;
105+
mod timeout;
103106
}
104107

105108
extension_trait! {
@@ -1084,6 +1087,40 @@ extension_trait! {
10841087
Skip::new(self, n)
10851088
}
10861089

1090+
#[doc=r#"
1091+
Await a stream or times out after a duration of time.
1092+
1093+
If you want to await an I/O future consider using
1094+
[`io::timeout`](../io/fn.timeout.html) instead.
1095+
1096+
# Examples
1097+
1098+
```
1099+
# fn main() -> std::io::Result<()> { async_std::task::block_on(async {
1100+
#
1101+
use std::time::Duration;
1102+
1103+
use async_std::stream;
1104+
use async_std::prelude::*;
1105+
1106+
let mut s = stream::repeat(1).take(3).timeout(Duration::from_secs(1));
1107+
1108+
while let Some(v) = s.next().await {
1109+
assert_eq!(v, Ok(1));
1110+
}
1111+
#
1112+
# Ok(()) }) }
1113+
```
1114+
"#]
1115+
#[cfg(any(feature = "unstable", feature = "docs"))]
1116+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
1117+
fn timeout(self, dur: Duration) -> Timeout<Self>
1118+
where
1119+
Self: Stream + Sized,
1120+
{
1121+
Timeout::new(self, dur)
1122+
}
1123+
10871124
#[doc = r#"
10881125
A combinator that applies a function as long as it returns successfully, producing a single, final value.
10891126
Immediately returns the error when the function returns unsuccessfully.

src/stream/stream/timeout.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::error::Error;
2+
use std::fmt;
3+
use std::pin::Pin;
4+
use std::time::Duration;
5+
6+
use futures_timer::Delay;
7+
use pin_project_lite::pin_project;
8+
9+
use crate::future::Future;
10+
use crate::stream::Stream;
11+
use crate::task::{Context, Poll};
12+
13+
pin_project! {
14+
/// A stream with timeout time set
15+
#[derive(Debug)]
16+
pub struct Timeout<S: Stream> {
17+
#[pin]
18+
stream: S,
19+
#[pin]
20+
delay: Delay,
21+
}
22+
}
23+
24+
impl<S: Stream> Timeout<S> {
25+
pub fn new(stream: S, dur: Duration) -> Timeout<S> {
26+
let delay = Delay::new(dur);
27+
28+
Timeout { stream, delay }
29+
}
30+
}
31+
32+
impl<S: Stream> Stream for Timeout<S> {
33+
type Item = Result<S::Item, TimeoutError>;
34+
35+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
36+
let this = self.project();
37+
38+
match this.stream.poll_next(cx) {
39+
Poll::Ready(Some(v)) => Poll::Ready(Some(Ok(v))),
40+
Poll::Ready(None) => Poll::Ready(None),
41+
Poll::Pending => match this.delay.poll(cx) {
42+
Poll::Ready(_) => Poll::Ready(Some(Err(TimeoutError { _private: () }))),
43+
Poll::Pending => Poll::Pending,
44+
},
45+
}
46+
}
47+
}
48+
49+
/// An error returned when a stream times out.
50+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
51+
#[cfg(any(feature = "unstable", feature = "docs"))]
52+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
53+
pub struct TimeoutError {
54+
_private: (),
55+
}
56+
57+
impl Error for TimeoutError {}
58+
59+
impl fmt::Display for TimeoutError {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61+
"stream has timed out".fmt(f)
62+
}
63+
}

0 commit comments

Comments
 (0)