Function ctx::with_timeout [] [src]

pub fn with_timeout(
    parent: Context,
    timeout: Duration
) -> (Context, Box<Fn() + Send>)

Returns a copy of the parent context with the given deadline associated to it. The returned context's future resolves when the deadline expires, the returned cancel function is called, or when the parent context's future resolves – whichever happens first.

Example

extern crate ctx;
extern crate futures;

use std::time::Duration;
use std::thread;
use ctx::{Context, ContextError, with_timeout, background};
use futures::future::Future;

fn main() {
    let (ctx, _) = with_timeout(background(), Duration::new(0, 50));
    thread::sleep(Duration::from_millis(100));

    assert_eq!(ctx.wait().unwrap_err(), ContextError::DeadlineExceeded);
}