commit
7094c4e619
2 changed files with 12 additions and 2 deletions
|
@ -2,7 +2,7 @@ use core::future::Future;
|
|||
use core::marker::PhantomData;
|
||||
use core::pin::Pin;
|
||||
use core::task::{Context, Poll};
|
||||
use futures::Stream;
|
||||
use futures::{future::select, future::Either, pin_mut, Stream};
|
||||
|
||||
use super::raw;
|
||||
use crate::time::{Duration, Instant};
|
||||
|
@ -31,6 +31,16 @@ impl crate::traits::delay::Delay for Delay {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct TimeoutError;
|
||||
pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> {
|
||||
let timeout_fut = Timer::after(timeout);
|
||||
pin_mut!(fut);
|
||||
match select(fut, timeout_fut).await {
|
||||
Either::Left((r, _)) => Ok(r),
|
||||
Either::Right(_) => Err(TimeoutError),
|
||||
}
|
||||
}
|
||||
|
||||
/// A future that completes at a specified [Instant](struct.Instant.html).
|
||||
pub struct Timer {
|
||||
expires_at: Instant,
|
||||
|
|
|
@ -5,7 +5,7 @@ mod duration;
|
|||
mod instant;
|
||||
mod traits;
|
||||
|
||||
pub use crate::executor::timer::{Delay, Ticker, Timer};
|
||||
pub use crate::executor::timer::{with_timeout, Delay, Ticker, TimeoutError, Timer};
|
||||
pub use duration::Duration;
|
||||
pub use instant::Instant;
|
||||
pub use traits::*;
|
||||
|
|
Loading…
Reference in a new issue