-
Notifications
You must be signed in to change notification settings - Fork 339
Add future::delay #349
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 future::delay #349
Changes from 3 commits
054f4fa
b251fc9
358d2bc
9d55fff
53fa132
5c9cfb4
d97b3df
613895d
c7dc147
1545d24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,15 @@ cfg_if::cfg_if! { | |
} | ||
} | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(any(feature = "unstable", feature = "docs"))] { | ||
mod delay; | ||
|
||
use std::time::Duration; | ||
use delay::DelayFuture; | ||
} | ||
} | ||
|
||
extension_trait! { | ||
#[doc = r#" | ||
A future represents an asynchronous computation. | ||
|
@@ -105,6 +114,28 @@ extension_trait! { | |
} | ||
|
||
pub trait FutureExt: std::future::Future { | ||
/// Creates a future that is delayed before it starts yielding items. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # async_std::task::block_on(async { | ||
/// use async_std::future; | ||
/// use std::time::Duration; | ||
/// use async_std::future::FutureExt; | ||
/// | ||
/// let a = future::ready(1).delay(Duration::from_millis(2000)); | ||
/// dbg!(a.await); | ||
/// # }) | ||
/// ``` | ||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] | ||
#[cfg(any(feature = "unstable", feature = "docs"))] | ||
fn delay(self, dur: Duration) -> DelayFuture<Self> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should obfuscate the type here, as we're awaiting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this? 53fa132 I don't know why I'm doing this, can you give me an explanation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a concrete type is returned from a trait, people might start relying on it, making this a compatibility hazard towards a future |
||
where | ||
Self: Future + Sized | ||
{ | ||
DelayFuture::new(self, dur) | ||
} | ||
} | ||
|
||
impl<F: Future + Unpin + ?Sized> Future for Box<F> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::pin::Pin; | ||
use std::time::Duration; | ||
|
||
use futures_timer::Delay; | ||
|
||
use crate::future::Future; | ||
use crate::task::{Context, Poll}; | ||
|
||
#[doc(hidden)] | ||
k-nasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[derive(Debug)] | ||
k-nasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct DelayFuture<F> { | ||
future: F, | ||
delay: Delay, | ||
} | ||
|
||
impl<F> DelayFuture<F> { | ||
pin_utils::unsafe_pinned!(future: F); | ||
pin_utils::unsafe_pinned!(delay: Delay); | ||
|
||
pub fn new(future: F, dur: Duration) -> DelayFuture<F> { | ||
let delay = Delay::new(dur); | ||
|
||
DelayFuture { future, delay } | ||
} | ||
} | ||
|
||
impl<F: Future> Future for DelayFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
match self.as_mut().delay().poll(cx) { | ||
Poll::Pending => Poll::Pending, | ||
Poll::Ready(_) => match self.future().poll(cx) { | ||
Poll::Ready(v) => Poll::Ready(v), | ||
Poll::Pending => Poll::Pending, | ||
}, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the implementation, I'm not sure if this documentation is correct.
To my reading, it delays execution of the future for the time time given. It will never be polled before the delay resolves. (which might or might not be what you want)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. I'm not fluent in English, so it might still be wrong ...