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.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
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