Description
In #212 (comment) I mentioned it'd be interesting to investigate broadcasting channels. I initially thought we might want to create a new type for this, possibly in a separate library, but I think at least API-wise we could integrate this into the existing channels module.
The way we could do this is by introducing a new API: Sender::send_all
. When an item is passed into Sender::send_all
it's cloned and sent to every available Receiver
.
Now a huge caveat here is that this may not at all be performant, and/or complicate the channels design to such a degree that a separate type is warranted. But I figured at least sharing what seems to be an ideal solution from an end-user point of view would provide us with a starting point to assess this further.
API
impl<T: Clone> Sender<T> {
pub async fn send_all<'_>(&'_ self, msg: T);
}
Example
use async_std::channel;
let (s, r) = sync::channel(1);
let a = task::spawn(async { dbg!(r.recv().await) });
let b = task::spawn(async { dbg!(r.recv().await) });
let c = task::spawn(async { dbg!(r.recv().await) });
s.send_all(String::from("chashu")).await;
a.join(b).join(c).await;
// prints: "chashu", "chashu", "chashu"