pub struct Timer { /* private fields */ }
Expand description
A future or stream that emits timed events.
Timers are futures that output a single Instant
when they fire.
Timers are also streams that can output Instant
s periodically.
§Precision
There is a limit on the maximum precision that a Timer
can provide. This limit is
dependent on the current platform; for instance, on Windows, the maximum precision is
about 16 milliseconds. Because of this limit, the timer may sleep for longer than the
requested duration. It will never sleep for less.
§Examples
Sleep for 1 second:
use async_io::Timer;
use std::time::Duration;
Timer::after(Duration::from_secs(1)).await;
Timeout after 1 second:
use async_io::Timer;
use futures_lite::FutureExt;
use std::time::Duration;
let addrs = async_net::resolve("google.com:80")
.or(async {
Timer::after(Duration::from_secs(1)).await;
Err(std::io::ErrorKind::TimedOut.into())
})
.await?;
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn never() -> Timer ⓘ
pub fn never() -> Timer ⓘ
Creates a timer that will never fire.
§Examples
This function may also be useful for creating a function with an optional timeout.
use async_io::Timer;
use futures_lite::prelude::*;
use std::time::Duration;
async fn run_with_timeout(timeout: Option<Duration>) {
let timer = timeout
.map(|timeout| Timer::after(timeout))
.unwrap_or_else(Timer::never);
run_lengthy_operation().or(timer).await;
}
// Times out after 5 seconds.
run_with_timeout(Some(Duration::from_secs(5))).await;
// Does not time out.
run_with_timeout(None).await;
Sourcepub fn after(duration: Duration) -> Timer ⓘ
pub fn after(duration: Duration) -> Timer ⓘ
Creates a timer that emits an event once after the given duration of time.
§Examples
use async_io::Timer;
use std::time::Duration;
Timer::after(Duration::from_secs(1)).await;
Sourcepub fn at(instant: Instant) -> Timer ⓘ
pub fn at(instant: Instant) -> Timer ⓘ
Creates a timer that emits an event once at the given time instant.
§Examples
use async_io::Timer;
use std::time::{Duration, Instant};
let now = Instant::now();
let when = now + Duration::from_secs(1);
Timer::at(when).await;
Sourcepub fn interval(period: Duration) -> Timer ⓘ
pub fn interval(period: Duration) -> Timer ⓘ
Creates a timer that emits events periodically.
§Examples
use async_io::Timer;
use futures_lite::StreamExt;
use std::time::{Duration, Instant};
let period = Duration::from_secs(1);
Timer::interval(period).next().await;
Sourcepub fn interval_at(start: Instant, period: Duration) -> Timer ⓘ
pub fn interval_at(start: Instant, period: Duration) -> Timer ⓘ
Creates a timer that emits events periodically, starting at start
.
§Examples
use async_io::Timer;
use futures_lite::StreamExt;
use std::time::{Duration, Instant};
let start = Instant::now();
let period = Duration::from_secs(1);
Timer::interval_at(start, period).next().await;
Sourcepub fn will_fire(&self) -> bool
pub fn will_fire(&self) -> bool
Indicates whether or not this timer will ever fire.
never()
will never fire, and timers created with after()
or at()
will fire
if the duration is not too large.
§Examples
use async_io::Timer;
use futures_lite::prelude::*;
use std::time::Duration;
// `never` will never fire.
assert!(!Timer::never().will_fire());
// `after` will fire if the duration is not too large.
assert!(Timer::after(Duration::from_secs(1)).will_fire());
assert!(!Timer::after(Duration::MAX).will_fire());
// However, once an `after` timer has fired, it will never fire again.
let mut t = Timer::after(Duration::from_secs(1));
assert!(t.will_fire());
(&mut t).await;
assert!(!t.will_fire());
// Interval timers will fire periodically.
let mut t = Timer::interval(Duration::from_secs(1));
assert!(t.will_fire());
t.next().await;
assert!(t.will_fire());
Sourcepub fn set_after(&mut self, duration: Duration)
pub fn set_after(&mut self, duration: Duration)
Sets the timer to emit an event once after the given duration of time.
Note that resetting a timer is different from creating a new timer because
set_after()
does not remove the waker associated with the task
that is polling the timer.
§Examples
use async_io::Timer;
use std::time::Duration;
let mut t = Timer::after(Duration::from_secs(1));
t.set_after(Duration::from_millis(100));
Sourcepub fn set_at(&mut self, instant: Instant)
pub fn set_at(&mut self, instant: Instant)
Sets the timer to emit an event once at the given time instant.
Note that resetting a timer is different from creating a new timer because
set_at()
does not remove the waker associated with the task
that is polling the timer.
§Examples
use async_io::Timer;
use std::time::{Duration, Instant};
let mut t = Timer::after(Duration::from_secs(1));
let now = Instant::now();
let when = now + Duration::from_secs(1);
t.set_at(when);
Sourcepub fn set_interval(&mut self, period: Duration)
pub fn set_interval(&mut self, period: Duration)
Sets the timer to emit events periodically.
Note that resetting a timer is different from creating a new timer because
set_interval()
does not remove the waker associated with the
task that is polling the timer.
§Examples
use async_io::Timer;
use futures_lite::StreamExt;
use std::time::{Duration, Instant};
let mut t = Timer::after(Duration::from_secs(1));
let period = Duration::from_secs(2);
t.set_interval(period);
Sourcepub fn set_interval_at(&mut self, start: Instant, period: Duration)
pub fn set_interval_at(&mut self, start: Instant, period: Duration)
Sets the timer to emit events periodically, starting at start
.
Note that resetting a timer is different from creating a new timer because
set_interval_at()
does not remove the waker associated with
the task that is polling the timer.
§Examples
use async_io::Timer;
use futures_lite::StreamExt;
use std::time::{Duration, Instant};
let mut t = Timer::after(Duration::from_secs(1));
let start = Instant::now();
let period = Duration::from_secs(2);
t.set_interval_at(start, period);
Trait Implementations§
Source§impl Stream for Timer
impl Stream for Timer
Auto Trait Implementations§
impl Freeze for Timer
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<F> FutureExt for F
impl<F> FutureExt for F
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Source§impl<S> StreamExt for S
impl<S> StreamExt for S
Source§fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
fn next(&mut self) -> NextFuture<'_, Self>where
Self: Unpin,
Source§fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
fn try_next<T, E>(&mut self) -> TryNextFuture<'_, Self>
Source§fn count(self) -> CountFuture<Self>where
Self: Sized,
fn count(self) -> CountFuture<Self>where
Self: Sized,
Source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
Source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
Source§fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
fn then<F, Fut>(self, f: F) -> Then<Self, F, Fut>
Source§fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
Source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the stream. Read moreSource§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
Source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the stream. Read moreSource§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
Source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
step
th item. Read moreSource§fn chain<U>(self, other: U) -> Chain<Self, U>
fn chain<U>(self, other: U) -> Chain<Self, U>
Source§fn collect<C>(self) -> CollectFuture<Self, C>
fn collect<C>(self) -> CollectFuture<Self, C>
Source§fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
fn try_collect<T, E, C>(self) -> TryCollectFuture<Self, C>
Source§fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
fn partition<B, P>(self, predicate: P) -> PartitionFuture<Self, P, B>
predicate
is true
and those for which it is
false
, and then collects them into two collections. Read moreSource§fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
fn fold<T, F>(self, init: T, f: F) -> FoldFuture<Self, F, T>
Source§fn try_fold<T, E, F, B>(
&mut self,
init: B,
f: F,
) -> TryFoldFuture<'_, Self, F, B>
fn try_fold<T, E, F, B>( &mut self, init: B, f: F, ) -> TryFoldFuture<'_, Self, F, B>
Source§fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
Source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
(index, item)
. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
fn nth(&mut self, n: usize) -> NthFuture<'_, Self>where
Self: Unpin,
n
th item of the stream. Read more