Function crossbeam_channel::after[][src]

Important traits for Receiver<T>
pub fn after(duration: Duration) -> Receiver<Instant>

Creates a receiver that delivers a message after a certain duration of time.

The channel is bounded with capacity of 1 and is never closed. Exactly one message will be automatically sent into the channel after duration elapses. The message is the instant at which it is sent into the channel.


use std::time::Duration;
use crossbeam_channel as channel;

let (s, r) = channel::unbounded::<i32>();

let timeout = Duration::from_millis(100);
select! {
    recv(r, msg) => println!("got {:?}", msg),
    recv(channel::after(timeout)) => println!("timed out"),
use std::thread;
use std::time::{Duration, Instant};
use crossbeam_channel as channel;

// Converts a number into a `Duration` in milliseconds.
let ms = |ms| Duration::from_millis(ms);

// Returns `true` if `a` and `b` are very close `Instant`s.
let eq = |a, b| a + ms(50) > b && b + ms(50) > a;

let start = Instant::now();
let r = channel::after(ms(100));


// This message was sent 100 ms from the start and received 500 ms from the start.
assert!(eq(r.recv().unwrap(), start + ms(100)));
assert!(eq(Instant::now(), start + ms(500)));