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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more