async-rs / async-rs/async-std

API: channel::send_all?

Open
#486 6 comments 6 reactions 0 assignees View on GitHub
api design
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.