API: channel::send_all?
- Dominant language
- Rust
- Stars
- 4.1k
- Forks
- 339
- PR merge metrics
- No merged PRs in 30d
Description
In https://github.com/async-rs/async-std/issues/212#issuecomment-535461575 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.
Related #436, cc/ @stjepang
## API
```rust
impl Sender {
pub async fn send_all<'_>(&'_ self, msg: T);
}
```
## Example
```rust
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"
```
Contributor guide
Assessment
This issue has not been assessed yet.