Function flume::bounded

source ·
pub fn bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>)
Expand description

Create a channel with a maximum capacity.

Create a bounded channel with a Sender and Receiver connected to each end respectively. Values sent in one end of the channel will be received on the other end. The channel is thread-safe, and both Sender and Receiver may be sent to or shared between threads as necessary. In addition, both Sender and Receiver may be cloned.

Unlike an unbounded channel, if there is no space left for new messages, calls to Sender::send will block (unblocking once a receiver has made space). If blocking behaviour is not desired, Sender::try_send may be used.

Like std::sync::mpsc, flume supports ‘rendezvous’ channels. A bounded queue with a maximum capacity of zero will block senders until a receiver is available to take the value. You can imagine a rendezvous channel as a ‘Glienicke Bridge’-style location at which senders and receivers perform a handshake and transfer ownership of a value.

§Examples

let (tx, rx) = flume::bounded(32);

for i in 1..33 {
    tx.send(i).unwrap();
}
assert!(tx.try_send(33).is_err());

assert_eq!(rx.try_iter().sum::<u32>(), (1..33).sum());