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();Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for BroadcastSender<T>
impl<T> !RefUnwindSafe for BroadcastSender<T>
impl<T> Send for BroadcastSender<T>
impl<T> Sync for BroadcastSender<T>
impl<T> Unpin for BroadcastSender<T>
impl<T> UnsafeUnpin for BroadcastSender<T>
impl<T> !UnwindSafe for BroadcastSender<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more