-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from yoshuawuyts/futures-time
Add `{Future,Stream}::wait_until`
- Loading branch information
Showing
7 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 core::future::Future; | ||
use core::pin::Pin; | ||
use core::task::{ready, Context, Poll}; | ||
|
||
/// Suspends a future until the specified deadline. | ||
/// | ||
/// This `struct` is created by the [`wait_until`] method on [`FutureExt`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`wait_until`]: crate::future::FutureExt::wait_until | ||
/// [`FutureExt`]: crate::future::FutureExt | ||
#[derive(Debug)] | ||
#[pin_project::pin_project] | ||
#[must_use = "futures do nothing unless polled or .awaited"] | ||
pub struct WaitUntil<F, D> { | ||
#[pin] | ||
future: F, | ||
#[pin] | ||
deadline: D, | ||
state: State, | ||
} | ||
|
||
/// The internal state | ||
#[derive(Debug)] | ||
enum State { | ||
Started, | ||
PollFuture, | ||
Completed, | ||
} | ||
|
||
impl<F, D> WaitUntil<F, D> { | ||
pub(super) fn new(future: F, deadline: D) -> Self { | ||
Self { | ||
future, | ||
deadline, | ||
state: State::Started, | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future, D: Future> Future for WaitUntil<F, D> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let mut this = self.project(); | ||
loop { | ||
match this.state { | ||
State::Started => { | ||
ready!(this.deadline.as_mut().poll(cx)); | ||
*this.state = State::PollFuture; | ||
} | ||
State::PollFuture => { | ||
let value = ready!(this.future.as_mut().poll(cx)); | ||
*this.state = State::Completed; | ||
return Poll::Ready(value); | ||
} | ||
State::Completed => panic!("future polled after completing"), | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,63 @@ | ||
use core::future::Future; | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
|
||
use futures_core::stream::Stream; | ||
use pin_project::pin_project; | ||
|
||
/// Delay execution of a stream once for the specified duration. | ||
/// | ||
/// This `struct` is created by the [`wait_until`] method on [`StreamExt`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`wait_until`]: crate::stream::StreamExt::wait_until | ||
/// [`StreamExt`]: crate::stream::StreamExt | ||
#[derive(Debug)] | ||
#[must_use = "streams do nothing unless polled or .awaited"] | ||
#[pin_project] | ||
pub struct WaitUntil<S, D> { | ||
#[pin] | ||
stream: S, | ||
#[pin] | ||
deadline: D, | ||
state: State, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum State { | ||
Timer, | ||
Streaming, | ||
} | ||
|
||
impl<S, D> WaitUntil<S, D> { | ||
pub(crate) fn new(stream: S, deadline: D) -> Self { | ||
WaitUntil { | ||
stream, | ||
deadline, | ||
state: State::Timer, | ||
} | ||
} | ||
} | ||
|
||
impl<S, D> Stream for WaitUntil<S, D> | ||
where | ||
S: Stream, | ||
D: Future, | ||
{ | ||
type Item = S::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let this = self.project(); | ||
|
||
match this.state { | ||
State::Timer => match this.deadline.poll(cx) { | ||
Poll::Pending => Poll::Pending, | ||
Poll::Ready(_) => { | ||
*this.state = State::Streaming; | ||
this.stream.poll_next(cx) | ||
} | ||
}, | ||
State::Streaming => this.stream.poll_next(cx), | ||
} | ||
} | ||
} |