mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-16 18:59:55 +00:00
Adding timeout extension method to Future trait
This commit is contained in:
parent
50cefce803
commit
c1f7be5d42
2 changed files with 20 additions and 1 deletions
|
@ -15,6 +15,7 @@ cfg_unstable! {
|
||||||
use try_race::TryRace;
|
use try_race::TryRace;
|
||||||
use join::Join;
|
use join::Join;
|
||||||
use try_join::TryJoin;
|
use try_join::TryJoin;
|
||||||
|
use crate::future::timeout::TimeoutFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
extension_trait! {
|
extension_trait! {
|
||||||
|
@ -355,6 +356,18 @@ extension_trait! {
|
||||||
{
|
{
|
||||||
TryJoin::new(self, other)
|
TryJoin::new(self, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc = r#"
|
||||||
|
Waits for both the future and a timeout, if the timeout completes before
|
||||||
|
the future, it returns an TimeoutError.
|
||||||
|
"#]
|
||||||
|
#[cfg(any(feature = "unstable", feature = "docs"))]
|
||||||
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
||||||
|
fn timeout<F, T>(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
|
||||||
|
where Self: Sized
|
||||||
|
{
|
||||||
|
TimeoutFuture::new(self, dur)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Future + Unpin + ?Sized> Future for Box<F> {
|
impl<F: Future + Unpin + ?Sized> Future for Box<F> {
|
||||||
|
|
|
@ -42,7 +42,7 @@ where
|
||||||
|
|
||||||
pin_project! {
|
pin_project! {
|
||||||
/// A future that times out after a duration of time.
|
/// A future that times out after a duration of time.
|
||||||
struct TimeoutFuture<F> {
|
pub struct TimeoutFuture<F> {
|
||||||
#[pin]
|
#[pin]
|
||||||
future: F,
|
future: F,
|
||||||
#[pin]
|
#[pin]
|
||||||
|
@ -50,6 +50,12 @@ pin_project! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<F> TimeoutFuture<F> {
|
||||||
|
pub fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
|
||||||
|
TimeoutFuture { future: future, delay: Delay::new(dur) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<F: Future> Future for TimeoutFuture<F> {
|
impl<F: Future> Future for TimeoutFuture<F> {
|
||||||
type Output = Result<F::Output, TimeoutError>;
|
type Output = Result<F::Output, TimeoutError>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue