relay_system

Struct BroadcastChannel

source
pub struct BroadcastChannel<T>
where T: Clone,
{ /* private fields */ }
Expand description

A channel that broadcasts values to attached senders.

This is part of the BroadcastResponse message behavior to efficiently send delayed responses to a large number of senders. All requests that are attached to this channel via their senders resolve with the same value.

§Example

use relay_system::{BroadcastChannel, BroadcastSender};

struct MyService {
    channel: Option<BroadcastChannel<String>>,
}

impl MyService {
    fn handle_message(&mut self, sender: BroadcastSender<String>) {
        if let Some(ref mut channel) = self.channel {
            channel.attach(sender);
        } else {
            self.channel = Some(sender.into_channel());
        }
    }

    fn finish_compute(&mut self, value: String) {
        if let Some(channel) = self.channel.take() {
            channel.send(value);
        }
    }
}

Implementations§

source§

impl<T: Clone> BroadcastChannel<T>

source

pub fn new() -> Self

Creates a standalone channel.

Use attach to add senders to this channel. Alternatively, create a channel with BroadcastSender::into_channel.

source

pub fn attach(&mut self, sender: BroadcastSender<T>)

Attaches a sender of another message to this channel to receive the same value.

§Example
use relay_system::{BroadcastChannel, BroadcastResponse, BroadcastSender};

// This is usually done as part of `Addr::send`
let (sender, rx) = BroadcastResponse::<&str>::channel();

let mut channel = BroadcastChannel::new();
channel.attach(sender);
source

pub fn send(self, value: T)

Sends a value to all attached senders and closes the channel.

This method succeeds even if no senders are attached to this channel anymore. To check if this channel is still active with senders attached, use is_attached.

§Example
use relay_system::BroadcastResponse;

// This is usually done as part of `Addr::send`
let (sender, rx) = BroadcastResponse::<&str>::channel();

let channel = sender.into_channel();
channel.send("test");
assert_eq!(rx.await, Ok("test"));
source

pub fn is_attached(&self) -> bool

Returns true if there are requests waiting for this channel.

The channel is not permanently closed when all waiting requests have detached. A new sender can be attached using attach even after this method returns false.

§Example
use relay_system::BroadcastResponse;

// This is usually done as part of `Addr::send`
let (sender, rx) = BroadcastResponse::<&str>::channel();

let channel = sender.into_channel();
assert!(channel.is_attached());

drop(rx);
assert!(!channel.is_attached());

Trait Implementations§

source§

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

source§

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

Formats the value using the given formatter. Read more
source§

impl<T: Clone> Default for BroadcastChannel<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BroadcastChannel<T>

§

impl<T> !RefUnwindSafe for BroadcastChannel<T>

§

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

§

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

§

impl<T> Unpin for BroadcastChannel<T>

§

impl<T> !UnwindSafe for BroadcastChannel<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