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

Stream::delay #309

Merged
merged 8 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions src/stream/stream/delay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

use pin_project_lite::pin_project;

use crate::stream::Stream;
use crate::task::{Context, Poll};

pin_project! {
#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct Delay<S> {
#[pin]
stream: S,
#[pin]
delay: futures_timer::Delay,
delay_done: bool,
}
}

impl<S> Delay<S> {
pub(super) fn new(stream: S, dur: Duration) -> Self {
Delay {
stream,
delay: futures_timer::Delay::new(dur),
delay_done: false,
}
}
}

impl<S> Stream for Delay<S>
where
S: Stream,
{
type Item = S::Item;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();

if !*this.delay_done {
futures_core::ready!(this.delay.poll(cx));
*this.delay_done = true;
}

this.stream.poll_next(cx)
}
}
43 changes: 43 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ cfg_unstable! {
pub use flat_map::FlatMap;
pub use timeout::{TimeoutError, Timeout};
pub use throttle::Throttle;
pub use delay::Delay;

mod count;
mod merge;
Expand All @@ -138,6 +139,7 @@ cfg_unstable! {
mod partition;
mod timeout;
mod throttle;
mod delay;
mod unzip;
}

Expand Down Expand Up @@ -573,6 +575,47 @@ extension_trait! {
Enumerate::new(self)
}

#[doc = r#"
Creates a stream that is delayed before it starts yielding items.

# Examples

```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
use std::time::{Duration, Instant};

let start = Instant::now();
let mut s = stream::from_iter(vec![0u8, 1, 2]).delay(Duration::from_millis(200));

assert_eq!(s.next().await, Some(0));
// The first time will take more than 200ms due to delay.
assert!(start.elapsed().as_millis() >= 200);

assert_eq!(s.next().await, Some(1));
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
// There will be no delay after the first time.
assert!(start.elapsed().as_millis() <= 210);

assert_eq!(s.next().await, Some(2));
assert!(start.elapsed().as_millis() <= 210);

assert_eq!(s.next().await, None);
assert!(start.elapsed().as_millis() <= 210);
#
# }) }
```
"#]
#[cfg(any(feature = "unstable", feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn delay(self, dur: std::time::Duration) -> Delay<Self>
where
Self: Sized,
{
Delay::new(self, dur)
}

#[doc = r#"
Takes a closure and creates a stream that calls that closure on every element of this stream.

Expand Down