Skip to main content

Broadcaster

Struct Broadcaster 

Source
pub struct Broadcaster<T> { /* private fields */ }
Expand description

A bounded, cancellable fan-out broadcaster.

Cheaply cloneable; every clone shares the same subscriber set, so an event broadcast through any clone reaches all live subscribers. T must be Clone (each subscriber receives its own copy) and Send + 'static to cross the per-subscriber channel.

Implementations§

Source§

impl<T> Broadcaster<T>
where T: Clone + Send + 'static,

Source

pub fn new() -> Self

Create a broadcaster with the DEFAULT_BROADCAST_BUFFER per-subscriber buffer.

Source

pub fn with_buffer(buffer: usize) -> Self

Create a broadcaster with an explicit per-subscriber buffer.

A buffer of zero is clamped to one so every subscriber can hold at least one in-flight event.

Source§

impl<T> Broadcaster<T>

Source

pub const fn buffer(&self) -> usize

The per-subscriber buffer capacity.

Source

pub fn subscriber_count(&self) -> usize

The number of currently registered subscribers.

Disconnected subscribers are pruned lazily on broadcast, so this count may transiently include subscribers whose streams have been dropped but not yet pruned.

Source

pub fn subscribe(&self, cancel: CancellationToken) -> BroadcastStream<T>
where T: Send + 'static,

Register a new subscriber and return its bounded, cancellable stream.

The returned stream yields events broadcast after subscription and terminates once cancel fires or the broadcaster is dropped. Pass a fresh CancellationToken when no external cancellation is required.

Source

pub fn broadcast(&self, item: &T)
where T: Clone,

Fan item out to every live subscriber.

Each subscriber receives an independent clone. A subscriber whose buffer is full is retained but skips this event (bounded drop backpressure); a subscriber whose stream has been dropped is pruned.

Trait Implementations§

Source§

impl<T> Clone for Broadcaster<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Broadcaster<T>

Source§

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

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

impl<T> Default for Broadcaster<T>
where T: Clone + Send + 'static,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Broadcaster<T>

§

impl<T> !UnwindSafe for Broadcaster<T>

§

impl<T> Freeze for Broadcaster<T>

§

impl<T> Send for Broadcaster<T>
where T: Send,

§

impl<T> Sync for Broadcaster<T>
where T: Send,

§

impl<T> Unpin for Broadcaster<T>

§

impl<T> UnsafeUnpin for Broadcaster<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.