reflux

Struct Router

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

An object that received data from a provided Receiver, and broadcasts the data to subscribers using a Round Robin algorithm.

Using a Router yields the following benefits:

  • Distribute data among multiple receivers, thus ensuring an even distribution of workload.

§Example

 #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
 #![feature(unboxed_closures)]
 use reflux::Router;
 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 stop_flag = Arc::new(AtomicBool::new(false));
         
 let (tx, rx) = unbounded();
         
 let mut router = Router::new(rx, None, stop_flag.clone());
         
 let (in1, out1) = unbounded();
 let (in2, out2) = unbounded();
         
 router.subscribe(in1);
 router.subscribe(in2);
         
 tx.send("hello".to_string()).unwrap();
 tx.send("there".to_string()).unwrap();
 tx.send("beautiful".to_string()).unwrap();
 tx.send("world".to_string()).unwrap();
         
 let out1_res = out1.recv().unwrap();
 let out2_res = out2.recv().unwrap();
 let out3_res = out1.recv().unwrap();
 let out4_res = out2.recv().unwrap();
         
 assert_eq!(out1_res, "hello".to_string());
 assert_eq!(out2_res, "there".to_string());
 assert_eq!(out3_res, "beautiful".to_string());
 assert_eq!(out4_res, "world".to_string());

Implementations§

Source§

impl<T> Router<T>
where T: Send + 'static,

Source

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

Creates a new Router object.

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

A Router object.

Source

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

Add a subscriber to the Router.

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

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

Waits for the Router object to finish execution.

Auto Trait Implementations§

§

impl<T> Freeze for Router<T>

§

impl<T> !RefUnwindSafe for Router<T>

§

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

§

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

§

impl<T> Unpin for Router<T>

§

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