crossbeam_channel

Function never

source
pub fn never<T>() -> Receiver<T>
Expand description

Creates a receiver that never delivers messages.

The channel is bounded with capacity of 0 and never gets disconnected.

ยงExamples

Using a never channel to optionally add a timeout to select!:

use std::thread;
use std::time::Duration;
use crossbeam_channel::{after, select, never, unbounded};

let (s, r) = unbounded();

thread::spawn(move || {
    thread::sleep(Duration::from_secs(1));
    s.send(1).unwrap();
});

// Suppose this duration can be a `Some` or a `None`.
let duration = Some(Duration::from_millis(100));

// Create a channel that times out after the specified duration.
let timeout = duration
    .map(|d| after(d))
    .unwrap_or(never());

select! {
    recv(r) -> msg => assert_eq!(msg, Ok(1)),
    recv(timeout) -> _ => println!("timed out"),
}