[][src]Trait sonr::reactor::Reactor

pub trait Reactor: Sized {
    type Output;
    type Input;
    fn react(
        &mut self,
        reaction: Reaction<Self::Input>
    ) -> Reaction<Self::Output>; fn chain<T: Reactor>(self, to: T) -> Chain<Self, T> { ... }
fn and<C: Reactor>(self, second: C) -> And<Self, C> { ... }
fn map<F, T>(self, callback: F) -> Map<Self, F, T> { ... }
fn or<T: Reactor>(self, second: T) -> Or<Self, T> { ... } }

A reactor reacts to a Reaction and returns a Reaction.

With the Output type of one reactor being the same type as Input of another it's possible to chain these two reactors together.

Associated Types

type Output

The output passed to the next reactor in the chain.

type Input

Expected input type from the previous reactor in the chain.

Loading content...

Required methods

fn react(&mut self, reaction: Reaction<Self::Input>) -> Reaction<Self::Output>

The generated output is passed as the input to the next reactor in the chain.

react is called repeatedly until the reaction returns Reaction::Continue

Loading content...

Provided methods

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>

Chain two reactors together. The output of the first reactor is the input of the second reactor.

fn and<C: Reactor>(self, second: C) -> And<Self, C>

Run two reactors independent of each other.

use sonr::system::System;
use sonr::net::tcp::ReactiveTcpListener;

fn main() -> Result<()> {
    System::init();
    let first_listener = ReactiveTcpListener::bind("127.0.0.1:5000")?;
    let second_listener = ReactiveTcpListener::bind("127.0.0.1:5001")?;
    let server = first_listener.and(second_listener);
    System::start(server);
}

fn map<F, T>(self, callback: F) -> Map<Self, F, T>

Capture the output of a reactor in a closure.

// Create a listener, print the address every time
// the listener accepts a new connection, then push that
// connection onto a queue.
System::init();
let listener = tcp::ReactiveTcpListener::bind("127.0.0.1:5000")?;
let queue = ReactiveQueue::unbounded();
let server = listener.map(|(stream, addr)| {
    stream
}).chain(queue);
System::start(server);

fn or<T: Reactor>(self, second: T) -> Or<Self, T>

Pass the output from a reactor into one of two reactors depending on the output. Note that both Reactors in an or are required to have the same output, and it's only possible to chain two ored reactors if the reactor that does the chaining outputs Either.

 use sonr::reactor::consumers::Consume;
 use sonr::reactor::producers::Mono;
 use sonr::reactor::Either;

 fn main() -> Result<()> {
     let system_sig = System::init()?;
     let producer = Mono::new(1u32)?
         .map(|val| {
             if val == 1 {
                 Either::A(val)
             } else {
                 Either::B(val)
             }
         });

     let reactor_a = Consume::new();
     let reactor_b = Consume::new();
     let reactor = reactor_a.or(reactor_b)
         .map(|_| {
             system_sig.send(SystemEvent::Stop);
         });

     let run = producer.chain(reactor);

     System::start(run)?;
     Ok(())
 }
Loading content...

Implementors

impl Reactor for ReactiveTcpListener[src]

type Output = (TcpStream, SocketAddr)

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl Reactor for ReactiveUdsListener[src]

type Output = (UnixStream, SocketAddr)

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<F, T> Reactor for Chain<F, T> where
    F: Reactor,
    T: Reactor<Input = F::Output>, 
[src]

type Input = F::Input

type Output = T::Output

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<S, F, T> Reactor for Map<S, F, T> where
    S: Reactor,
    F: FnMut(S::Output) -> T, 
[src]

type Output = T

type Input = S::Input

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T> Reactor for Consume<T>[src]

type Input = T

type Output = T

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T> Reactor for Mono<T>[src]

type Input = ()

type Output = T

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T> Reactor for ReactiveGenerator<T>[src]

type Output = T

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T> Reactor for ReactiveDeque<T>[src]

type Output = T

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T, U> Reactor for And<T, U> where
    T: Reactor,
    U: Reactor
[src]

type Output = ()

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T: Reactor<Output = O>, U: Reactor<Output = O>, O> Reactor for Or<T, U>[src]

type Input = Either<T::Input, U::Input>

type Output = O

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T: Clone> Reactor for ReactiveBroadcast<T>[src]

type Output = ()

type Input = T

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T: Send + 'static> Reactor for ReactiveQueue<T>[src]

type Output = ()

type Input = T

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T: Send + 'static> Reactor for ReactiveSignalReceiver<T>[src]

type Output = T

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

impl<T: Read + Write + Evented> Reactor for Stream<T>[src]

type Output = ()

type Input = ()

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

Loading content...