Module tokio::timer [] [src]

Utilities for tracking time.

This module provides a number of types for executing code after a set period of time.

These types are sufficient for handling a large number of scenarios involving time.

These types must be used from within the context of the Runtime or a timer context must be setup explicitly. See the tokio-timer crate for more details on how to setup a timer context.

Examples

Wait 100ms and print "Hello World!"

use tokio::prelude::*;
use tokio::timer::Delay;

use std::time::{Duration, Instant};

let when = Instant::now() + Duration::from_millis(100);

tokio::run({
    Delay::new(when)
        .map_err(|e| panic!("timer failed; err={:?}", e))
        .and_then(|_| {
            println!("Hello world!");
            Ok(())
        })
})

Require that an operation takes no more than 300ms. Note that this uses the deadline function on the FutureExt trait. This trait is included in the prelude.

use tokio::prelude::*;

use std::time::{Duration, Instant};

fn long_op() -> Box<Future<Item = (), Error = ()> + Send> {
    // ...
}

let when = Instant::now()  + Duration::from_millis(300);

tokio::run({
    long_op()
        .deadline(when)
        .map_err(|e| {
            println!("operation timed out");
        })
})

Structs

Deadline

Allows a given Future to execute until the specified deadline.

DeadlineError

Error returned by Deadline future.

Delay

A future that completes at a specified instant in time.

Error

Errors encountered by the timer implementation.

Interval

A stream representing notifications at fixed interval