Struct Broadcast

Source
pub struct Broadcast<'a, C: ?Sized, T: ?Sized> { /* private fields */ }
Expand description

Event source that transmits context/value pairs to multiple observers.

In order to “fork” the broadcast (creating a new stream that will be subscribed to it), the broadcast object can be simply cloned via the Clone trait. Note that cloning the broadcast only increases its reference count; no values are being cloned or copied.

A broadcast may receive a value in one of two ways. First, the user may explicitly call one of its methods: send(), send_ctx(), feed(), feed_ctx(). Second, the broadcast may be created from a parent stream via broadcast() method of the stream object. Either way, each context/value pair received is passed on to each of the subscribed observers, by reference.

§Examples

let out = RefCell::new(Vec::new());
let stream = SimpleBroadcast::<i32>::new();
let child1 = stream
    .clone()
    .subscribe(|x| out.borrow_mut().push(*x + 1));
let child2 = stream
    .clone()
    .subscribe(|x| out.borrow_mut().push(*x + 7));
stream.feed(1..=3);
assert_eq!(&*out.borrow(), &[2, 8, 3, 9, 4, 10]);

Implementations§

Source§

impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Broadcast<'a, C, T>

Source

pub fn new() -> Self

Creates a new broadcast with specified context and item types.

Source

pub fn from_stream<S>(stream: S) -> Self
where S: Stream<'a, Context = C, Item = T>,

Create a broadcast from a stream, enabling multiple observers (“fork” the stream).

Note: this is equivalent to calling broadcast() on the stream object.

Source

pub fn send_ctx<K, B>(&self, ctx: K, value: B)
where K: Borrow<C>, B: Borrow<T>,

Send a value along with context to all observers of the broadcast.

Source

pub fn send<B>(&self, value: B)
where B: Borrow<T>, C: Default,

Similar to send_ctx(), but the context is set to the type’s default value.

Source

pub fn feed_ctx<K, B, I>(&self, ctx: K, iter: I)
where K: Borrow<C>, I: Iterator<Item = B>, B: Borrow<T>,

Convenience method to feed an iterator of values to all observers of the broadcast, along with a given context.

Source

pub fn feed<B, I>(&self, iter: I)
where I: Iterator<Item = B>, B: Borrow<T>, C: Default,

Similar to feed_ctx(), but the context is set to the type’s default value.

Trait Implementations§

Source§

impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Clone for Broadcast<'a, C, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Default for Broadcast<'a, C, T>

Source§

fn default() -> Self

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

impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Stream<'a> for Broadcast<'a, C, T>

Source§

type Context = C

The type of the context attached to emitted elements. Read more
Source§

type Item = T

The type of the elements being emitted.
Source§

fn subscribe_ctx<O>(self, observer: O)
where O: FnMut(&Self::Context, &Self::Item) + 'a,

Same as subscribe(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn subscribe<O>(self, observer: O)
where O: 'a + FnMut(&Self::Item),

Attaches an observer (a user-provided mutable closure) to the stream, which consumes the stream object. Read more
Source§

fn broadcast(self) -> Broadcast<'a, Self::Context, Self::Item>
where Self: 'a,

Create a broadcast from a stream, enabling multiple observers. This is the only Stream trait method that incurs a slight runtime cost, due to the broadcast object having to store observers as boxed trait objects in a reference-counted container; all other methods can be inlined. Read more
Source§

fn ctx(self) -> Context<Self>

Convenience method to extract the context into a separate stream. Read more
Source§

fn with_ctx<T>(self, ctx: T) -> WithContext<Self, T>

Set the context to a fixed constant value. Read more
Source§

fn with_ctx_map<F, T>(self, func: F) -> WithContextMap<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item) -> T,

Creates a new stream which calls a closure on each context/value and uses that as the context. Read more
Source§

fn map<F, T>(self, func: F) -> Map<Self, NoContext<F>>
where F: 'a + FnMut(&Self::Item) -> T,

Creates a new stream which calls a closure on each element and uses that as the value. Read more
Source§

fn map_ctx<F, T>(self, func: F) -> Map<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item) -> T,

Same as map(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn map_both<F, C, T>(self, func: F) -> MapBoth<Self, NoContext<F>>
where F: 'a + FnMut(&Self::Item) -> (C, T),

Same as map(), but the closure is expected to return a (context, value) tuple, so that both the context and the value can be changed at the same time. Read more
Source§

fn map_both_ctx<F, C, T>(self, func: F) -> MapBoth<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item) -> (C, T),

Same as map_both(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn filter<F>(self, func: F) -> Filter<Self, NoContext<F>>
where F: 'a + FnMut(&Self::Item) -> bool,

Creates a stream which uses a closure to determine if an element should be yielded. Read more
Source§

fn filter_ctx<F>(self, func: F) -> Filter<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item) -> bool,

Same as filter(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn filter_map<F, T>(self, func: F) -> FilterMap<Self, NoContext<F>>
where F: 'a + FnMut(&Self::Item) -> Option<T>,

Creates a stream that both filters and maps. Read more
Source§

fn filter_map_ctx<F, T>(self, func: F) -> FilterMap<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item) -> Option<T>,

Same as filter_map(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn fold<F, T: 'a>(self, init: T, func: F) -> Fold<Self, NoContext<F>, T>
where F: 'a + FnMut(&T, &Self::Item) -> T,

‘Reduce’ operation on streams. Read more
Source§

fn fold_ctx<F, T: 'a>(self, init: T, func: F) -> Fold<Self, F, T>
where F: 'a + FnMut(&Self::Context, &T, &Self::Item) -> T,

Same as fold(), but the closure receives three arguments (context/accumulator/value), by reference. Read more
Source§

fn inspect<F>(self, func: F) -> Inspect<Self, NoContext<F>>
where F: 'a + FnMut(&Self::Item),

Do something with each element of a stream, passing the value on. Read more
Source§

fn inspect_ctx<F>(self, func: F) -> Inspect<Self, F>
where F: 'a + FnMut(&Self::Context, &Self::Item),

Same as inspect(), but the closure receives two arguments (context/value), by reference. Read more
Source§

fn last_n(self, count: usize) -> LastN<Self, Self::Item>
where Self::Item: 'a + Clone + Sized,

Creates a stream that caches up to n last elements. Read more

Auto Trait Implementations§

§

impl<'a, C, T> Freeze for Broadcast<'a, C, T>
where C: ?Sized, T: ?Sized,

§

impl<'a, C, T> !RefUnwindSafe for Broadcast<'a, C, T>

§

impl<'a, C, T> !Send for Broadcast<'a, C, T>

§

impl<'a, C, T> !Sync for Broadcast<'a, C, T>

§

impl<'a, C, T> Unpin for Broadcast<'a, C, T>
where C: ?Sized, T: ?Sized,

§

impl<'a, C, T> !UnwindSafe for Broadcast<'a, C, 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.