pub trait TimedExt: Future {
// Provided methods
fn timeout(self, after: Duration) -> Timed<Self> ⓘ
where Self: Sized { ... }
fn deadline(self, deadline: Instant) -> Timed<Self> ⓘ
where Self: Sized { ... }
}Provided Methods§
Sourcefn timeout(self, after: Duration) -> Timed<Self> ⓘwhere
Self: Sized,
fn timeout(self, after: Duration) -> Timed<Self> ⓘwhere
Self: Sized,
Given a Duration, creates and returns a new Timed that will poll both the Future, and a Timer that will complete after the provided duration.
- first polling the Future, returns it’s output if any.
- then polling the Timer, and fallback to
Noneif the Timer completes.
§Example
use async_io::Timer;
use smoltimeout::TimeoutExt;
use std::time::Duration;
let mut timer = Timer::after(Duration::from_millis(250));
let foo = async {
timer.await;
24
}.timeout(Duration::from_millis(100));
assert_eq!(foo.await, None);
timer = Timer::after(Duration::from_millis(100));
let bar = async move {
timer.await;
42
}.timeout(Duration::from_millis(250));
assert_eq!(bar.await, Some(42));
timer = Timer::after(Duration::from_millis(50));
let mod_of_delta4 = async move {
timer.await;
(21*2) as f64
}.timeout(Duration::from_millis(0));
Timer::after(Duration::from_millis(100)).await;
assert_eq!(mod_of_delta4.await, Some(42.0));Sourcefn deadline(self, deadline: Instant) -> Timed<Self> ⓘwhere
Self: Sized,
fn deadline(self, deadline: Instant) -> Timed<Self> ⓘwhere
Self: Sized,
Given a Instant, creates and returns a new Timed that will poll both the Future
and a Timer that will complete after the provided deadline.
- first polling the Future, returns it’s output if any.
- then polling the Timer, and fallback to
Noneif the Timer completes.
§Example
use async_io::Timer;
use smoltimeout::TimeoutExt;
use std::time::{Instant, Duration};
let mut timer = Timer::after(Duration::from_millis(250));
let foo = async move {
timer.await;
24
}.deadline(Instant::now() + Duration::from_millis(100));
assert_eq!(foo.await, None);
timer = Timer::after(Duration::from_millis(100));
let bar = async move {
timer.await;
42
}.deadline(Instant::now() + Duration::from_millis(250));
assert_eq!(bar.await, Some(42));
timer = Timer::after(Duration::from_millis(50));
let mod_of_delta4 = async move {
timer.await;
(21*2) as f64
}.deadline(Instant::now());
Timer::after(Duration::from_millis(100)).await;
assert_eq!(mod_of_delta4.await, Some(42.0));