pub struct BroadcastSender<T>(/* private fields */)
where
T: Clone;
Expand description
Sends a message response from a service back to the waiting BroadcastRequest
.
The sender is part of an BroadcastResponse
and should be moved into the service interface
type. If this sender is dropped without calling send
, the request fails with
SendError
.
As opposed to the regular Sender
for asynchronous messages, this sender can be converted
into a channel that efficiently shares a common response for multiple
requests to the same data value. This is useful if resolving or computing the value takes more
time.
§Example
use relay_system::BroadcastResponse;
// This is usually done as part of `Addr::send`
let (sender1, rx1) = BroadcastResponse::<&str>::channel();
let (sender2, rx2) = BroadcastResponse::<&str>::channel();
// On the first time, convert the sender into a channel
let mut channel = sender1.into_channel();
// The second time, attach the sender to the existing channel
channel.attach(sender2);
// Send a value into the channel to resolve all requests simultaneously
channel.send("test");
assert_eq!(rx1.await, Ok("test"));
assert_eq!(rx2.await, Ok("test"));
Implementations§
source§impl<T: Clone> BroadcastSender<T>
impl<T: Clone> BroadcastSender<T>
sourcepub fn send(self, value: T)
pub fn send(self, value: T)
Immediately resolve a ready value.
This bypasses shared channels and directly sends the a value to the waiting
request. In terms of performance and behavior, using send
is
equivalent to calling Sender::send
for a regular AsyncResponse
.
§Example
use relay_system::BroadcastResponse;
// This is usually done as part of `Addr::send`
let (sender, rx) = BroadcastResponse::<&str>::channel();
// sender is NOT converted into a channel!
sender.send("test");
assert_eq!(rx.await, Ok("test"));
sourcepub fn into_channel(self) -> BroadcastChannel<T>
pub fn into_channel(self) -> BroadcastChannel<T>
Creates a channel from this sender that can be shared with other senders.
To add more senders to the created channel at a later point, use
attach
.
§Example
use relay_system::{BroadcastChannel, BroadcastResponse};
// This is usually done as part of `Addr::send`
let (sender, rx) = BroadcastResponse::<&str>::channel();
let channel: BroadcastChannel<&str> = sender.into_channel();