Struct async_io::Timer

source ·
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 Instants periodically.

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(10)).await;
        Err(std::io::ErrorKind::TimedOut.into())
    })
    .await?;

Implementations§

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;

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;

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;

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;

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;

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));

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);

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);

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§

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
The type of value produced on completion.
Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Values yielded by the stream.
Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

A convenience for calling Future::poll() on !Unpin types.
Returns the result of self or other future, preferring self if both are ready. Read more
Returns the result of self or other future, with no preference if both are ready. Read more
Catches panics while polling the future. Read more
Boxes the future and changes its type to dyn Future + Send + 'a. Read more
Boxes the future and changes its type to dyn Future + 'a. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The output that the future will produce on completion.
Which kind of future are we turning this into?
Creates a future from a value. Read more
A convenience for calling [Stream::poll_next()] on !Unpin types.
Retrieves the next item in the stream. Read more
Retrieves the next item in the stream. Read more
Counts the number of items in the stream. Read more
Maps items of the stream to new values using a closure. Read more
Maps items to streams and then concatenates them. Read more
Concatenates inner streams. Read more
Maps items of the stream to new values using an async closure. Read more
Keeps items of the stream for which predicate returns true. Read more
Filters and maps items of the stream using a closure. Read more
Takes only the first n items of the stream. Read more
Takes items while predicate returns true. Read more
Skips the first n items of the stream. Read more
Skips items while predicate returns true. Read more
Yields every stepth item. Read more
Appends another stream to the end of this one. Read more
Clones all items. Read more
Copies all items. Read more
Collects all items in the stream into a collection. Read more
Collects all items in the fallible stream into a collection. Read more
Partitions items into those for which predicate is true and those for which it is false, and then collects them into two collections. Read more
Accumulates a computation over the stream. Read more
Accumulates a fallible computation over the stream. Read more
Maps items of the stream to new values using a state value and a closure. Read more
Fuses the stream so that it stops yielding items after the first None. Read more
Repeats the stream from beginning to end, forever. Read more
Enumerates items, mapping them to (index, item). Read more
Calls a closure on each item and passes it on. Read more
Gets the nth item of the stream. Read more
Returns the last item in the stream. Read more
Finds the first item of the stream for which predicate returns true. Read more
Applies a closure to items in the stream and returns the first Some result. Read more
Finds the index of the first item of the stream for which predicate returns true. Read more
Tests if predicate returns true for all items in the stream. Read more
Tests if predicate returns true for any item in the stream. Read more
Calls a closure on each item of the stream. Read more
Calls a fallible closure on each item of the stream, stopping on first error. Read more
Zips up two streams into a single stream of pairs. Read more
Collects a stream of pairs into a pair of collections. Read more
Merges with other stream, preferring items from self whenever both streams are ready. Read more
Merges with other stream, with no preference for either stream when both are ready. Read more
Boxes the stream and changes its type to dyn Stream + Send + 'a. Read more
Boxes the stream and changes its type to dyn Stream + 'a. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.