roman-roman
roman-roman

Reputation: 2796

How to forward messages from a stream to a broadcast channel?

I'm trying to build a stream that should send its elements to two other streams, that are to be executed in parallel. I tried creating a broadcast channel with:

let (int_sender, int_receiver) = broadcast::channel(DEFAULT_CHANNEL_CAPACITY);    

and then doing stream.forward(int_sender), with two more streams built from the int_receiver, but it doesn't work because broadcast::Sender doesn't implement Sink (unlike mpsc::Sender). Are there any other options?

Upvotes: 1

Views: 1636

Answers (1)

apetranzilla
apetranzilla

Reputation: 5949

forward is just a convenience method for connecting a stream to a sink. You can implement the same functionality manually with code like the following:

while let Some(item) = stream.next().await {
    int_sender.send(item)?;
}

Upvotes: 1

Related Questions