Struct reactive_rs::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§

Creates a new broadcast with specified context and item types.

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

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

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

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

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

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

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more
The type of the context attached to emitted elements. Read more
The type of the elements being emitted.
Same as subscribe(), but the closure receives two arguments (context/value), by reference. Read more
Attaches an observer (a user-provided mutable closure) to the stream, which consumes the stream object. Read more
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
Convenience method to extract the context into a separate stream. Read more
Set the context to a fixed constant value. Read more
Creates a new stream which calls a closure on each context/value and uses that as the context. Read more
Creates a new stream which calls a closure on each element and uses that as the value. Read more
Same as map(), but the closure receives two arguments (context/value), by reference. Read more
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
Same as map_both(), but the closure receives two arguments (context/value), by reference. Read more
Creates a stream which uses a closure to determine if an element should be yielded. Read more
Same as filter(), but the closure receives two arguments (context/value), by reference. Read more
Creates a stream that both filters and maps. Read more
Same as filter_map(), but the closure receives two arguments (context/value), by reference. Read more
‘Reduce’ operation on streams. Read more
Same as fold(), but the closure receives three arguments (context/accumulator/value), by reference. Read more
Do something with each element of a stream, passing the value on. Read more
Same as inspect(), but the closure receives two arguments (context/value), by reference. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.