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 en 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§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
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,
§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 moresource§fn last(self) -> LastFuture<Self>where
Self: Sized,
fn last(self) -> LastFuture<Self>where
Self: Sized,
source§fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>
fn find<P>(&mut self, predicate: P) -> FindFuture<'_, Self, P>
source§fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
fn find_map<F, B>(&mut self, f: F) -> FindMapFuture<'_, Self, F>
source§fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
fn position<P>(&mut self, predicate: P) -> PositionFuture<'_, Self, P>
source§fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
fn for_each<F>(self, f: F) -> ForEachFuture<Self, F>
source§fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
fn try_for_each<F, E>(&mut self, f: F) -> TryForEachFuture<'_, Self, F>
source§fn zip<U>(self, other: U) -> Zip<Self, U>
fn zip<U>(self, other: U) -> Zip<Self, U>
source§fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
source§fn race<S>(self, other: S) -> Race<Self, S>
fn race<S>(self, other: S) -> Race<Self, S>
other
stream, with no preference for either stream when both are ready. Read more