Function crossbeam_channel::tick[][src]

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

Creates a receiver that delivers messages periodically.

The channel is bounded with capacity of 1 and is never closed. Messages will be automatically sent into the channel in intervals of duration, but the time intervals are only measured while the channel is empty. The channel always contains at most one message. Each message is the instant at which it is sent into the channel.

Examples

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::tick(ms(100));

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

thread::sleep(ms(500));

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

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