pub fn at(when: Instant) -> Receiver<Instant>
Expand description
Creates a receiver that delivers a message at a certain instant in time.
The channel is bounded with capacity of 1 and never gets disconnected. Exactly one message will
be sent into the channel at the moment in time when
. The message is the instant at which it
is sent, which is the same as when
. If when
is in the past, the message will be delivered
instantly to the receiver.
ยงExamples
Using an at
channel for timeouts:
use std::time::{Instant, Duration};
use crossbeam_channel::{at, select, unbounded};
let (s, r) = unbounded::<i32>();
let deadline = Instant::now() + Duration::from_millis(500);
select! {
recv(r) -> msg => println!("received {:?}", msg),
recv(at(deadline)) -> _ => println!("timed out"),
}
When the message gets sent:
use std::time::{Duration, Instant};
use crossbeam_channel::at;
// Converts a number of milliseconds into a `Duration`.
let ms = |ms| Duration::from_millis(ms);
let start = Instant::now();
let end = start + ms(100);
let r = at(end);
// This message was sent 100 ms from the start
assert_eq!(r.recv().unwrap(), end);
assert!(Instant::now() > start + ms(100));