Function async_broadcast::broadcast

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

Create a new broadcast channel.

The created channel has space to hold at most cap messages at a time.

Panics

Capacity must be a positive number. If cap is zero, this function will panic.

Examples

use async_broadcast::{broadcast, TryRecvError, TrySendError};

let (s, mut r1) = broadcast(1);
let mut r2 = r1.clone();

assert_eq!(s.broadcast(10).await, Ok(None));
assert_eq!(s.try_broadcast(20), Err(TrySendError::Full(20)));

assert_eq!(r1.recv().await, Ok(10));
assert_eq!(r2.recv().await, Ok(10));
assert_eq!(r1.try_recv(), Err(TryRecvError::Empty));
assert_eq!(r2.try_recv(), Err(TryRecvError::Empty));