new_channel_with_dependency

Function new_channel_with_dependency 

Source
pub fn new_channel_with_dependency<T, U>(
    capacity: Option<usize>,
    dependency_sender: &Sender<U>,
    dependency_receiver: &Receiver<U>,
) -> (Sender<T>, Receiver<T>)
Expand description

Creates a multi-producer multi-consumer channel of either bounded or unbounded capacity that ACTS disconnected when the channel it depends on is disconnected.

  • If capacity is None, this channel has a growable buffer that can hold any number of messages at a time.
  • If capacity is Some(n), this channel has a buffer that can hold at most n messages at a time.

A special case is zero-capacity channel, which cannot hold any messages. Instead, send and receive operations must appear at the same time in order to pair up and pass the message over.

ยงExamples

use std::thread;
use crossbeam_channel::SendError;
use lambda_channel::channel::{new_channel, new_channel_with_dependency};

let (s_b, r_b) = new_channel(None);
let (s_a, r_a) = new_channel_with_dependency(None, &s_b, &r_b);

fn fib(n: i32) -> u64 {
    if n <= 1 {
        n as u64
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

// Spawn an asynchronous computation.
let handle = thread::spawn(move || {
    while let Ok(v) = r_a.recv() {
        s_b.send(fib(v)).unwrap();
    }
});

s_a.send(20).unwrap();
assert_eq!(r_b.recv(), Ok(6765));

drop(r_b);
let _ = handle.join();
assert_eq!(s_a.send(10), Err(SendError(10)));