-
Notifications
You must be signed in to change notification settings - Fork 340
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
Merged
Merged
Add future::delay #349
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
054f4fa
feat: Add future::delay
k-nasa b251fc9
Move delay method to FutureExt::delay
k-nasa 358d2bc
Add import crate
k-nasa 9d55fff
fix export FutureExt
k-nasa 53fa132
fix type Declaration
k-nasa 5c9cfb4
Merge branch 'master' into add_future_delay
k-nasa d97b3df
fix: Remove Pin API related unsafe code
k-nasa 613895d
doc: fix documantation text
k-nasa c7dc147
fix indent
k-nasa 1545d24
Merge branch 'master' into add_future_delay
k-nasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::pin::Pin; | ||
use std::time::Duration; | ||
|
||
use futures_timer::Delay; | ||
|
||
use crate::future::Future; | ||
use crate::task::{Context, Poll}; | ||
|
||
/// 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; | ||
|
||
/// let a = future::delay(future::ready(1) ,Duration::from_millis(2000)); | ||
/// dbg!(a.await); | ||
/// # }) | ||
/// ``` | ||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] | ||
#[cfg(any(feature = "unstable", feature = "docs"))] | ||
pub fn delay<F>(f: F, dur: Duration) -> DelayFuture<F> | ||
where | ||
F: Future, | ||
{ | ||
DelayFuture::new(f, dur) | ||
} | ||
|
||
#[doc(hidden)] | ||
#[derive(Debug)] | ||
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, | ||
}, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.