relay_system

Struct BroadcastSender

source
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>

source

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"));
source

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§

source§

impl<T> Debug for BroadcastSender<T>
where T: Clone + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BroadcastSender<T>

§

impl<T> !RefUnwindSafe for BroadcastSender<T>

§

impl<T> Send for BroadcastSender<T>
where T: Send + Sync,

§

impl<T> Sync for BroadcastSender<T>
where T: Send + Sync,

§

impl<T> Unpin for BroadcastSender<T>

§

impl<T> !UnwindSafe for BroadcastSender<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T