Skip to content

API: channel::send_all? #486

Open
Open
@yoshuawuyts

Description

@yoshuawuyts

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.

Related #436, cc/ @stjepang

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    api designOpen design questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions