reflux

Struct Broadcast

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

An object that received data from a provided Receiver, and broadcasts the data to all subscribers.

Using a Broadcast yields the following benefits:

  • Send received data to multiple endpoint. A Broadcast object can be used to build a web of objects.

§Example

 #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
 #![feature(unboxed_closures)]
 use reflux::{Extractor, Loader, Broadcast};
 use std::sync::Arc;
 use std::sync::atomic::{AtomicBool, Ordering};
 use crossbeam_channel::Receiver;
 use reflux::add_routine;
 use crossbeam_channel::unbounded;
 use std::time::Duration;
 use std::thread::sleep;
 let stop_flag = Arc::new(AtomicBool::new(false));
 
 let test_inlet: (Extractor, Receiver<String>) = Extractor::new(add_routine!(#[coroutine] || {
            sleep(Duration::from_secs(1));
            yield "hello".to_string()
        }), stop_flag.clone(), None, (), None);
 
 let (test_outlet1_sink, test_outlet1_source) = unbounded();
 let (test_outlet2_sink, test_outlet2_source) = unbounded();
 
 let (test_outlet1, test1_tx) = Loader::new(move |example: String| {
    test_outlet1_sink.send(format!("1: {example}")).unwrap()
 }, None, stop_flag.clone(), None);
 
 let (test_outlet2, test2_tx) = Loader::new(move |example: String| {
     test_outlet2_sink.send(format!("2: {example}")).unwrap()
 }, None, stop_flag.clone(), None);
 
 let mut broadcaster = Broadcast::new(test_inlet.1, None, stop_flag.clone(), None);
 broadcaster.subscribe(test1_tx);
 broadcaster.subscribe(test2_tx);
 
 let data1 = test_outlet1_source.recv().unwrap();
 let data2 = test_outlet2_source.recv().unwrap();
 
 stop_flag.store(true, Ordering::Relaxed);
 
 test_outlet1.join().unwrap();
 test_outlet2.join().unwrap();
 test_inlet.0.join().unwrap();
 broadcaster.join().unwrap();
 
 
 assert_eq!(data1, "1: hello".to_string());
 assert_eq!(data2, "2: hello".to_string());

Implementations§

Source§

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

Source

pub fn new( source: Receiver<T>, pause_sig: Option<Arc<AtomicBool>>, stop_sig: Arc<AtomicBool>, data_limit: Option<usize>, ) -> Self

Creates a new Broadcast object.

§Parameters
  • source - The source of data that needs to be broadcast.
  • pause_sig - A flag to signal the Broadcast object to pause execution.
  • stop_sig - A flag to signal the Broadcast object to terminate execution.
§Returns

A Broadcast object.

Source

pub fn subscribe(&mut self, subscriber: Sender<T>)

Add a subscriber to the Broadcast

§Parameters
  • subscriber - A Sender with which to send data.
Source

pub fn channel(&mut self) -> Receiver<T>

Create a subscription for an external object to use to receive data from the Broadcast

§Returns
  • A Receiver channel from which to receive data.
Source

pub fn join(self) -> Result<()>

Waits for the Broadcast object to finish execution.

Auto Trait Implementations§

§

impl<T> Freeze for Broadcast<T>

§

impl<T> !RefUnwindSafe for Broadcast<T>

§

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

§

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

§

impl<T> Unpin for Broadcast<T>

§

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

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.