The Signals crate is an asynchronous functional-reactive-like api.
Add this to cargo.toml
[dependencies]
signals = "0.0.1"
Add this to your crate
extern crate signals;
```rust use signals::{Signal, Emitter};
fn main() { // create a signal that will assert when emitted let signal = Signal::new( |x: &u32| Ok(x) ); let listener = Signal::new( |x: &u32| { assert_ne!(x, 5); Ok(()) } ); //fail!
// when signal is emitted, listener should execute.
signal.lock().unwrap().register_listener(&listener);
// emit signal
signal.lock().unwrap().emit(5);
} ```
```rust use signals::{Signal, Emitter};
fn main() {
// create a bunch of signals. At the last signal, we should fail.
// If we do not fail, the signals did not work.
let root = Signal::new( |x: &u32| Ok((x).to_string()) ); //convert x to string.
let peek = Signal::new( |x: &String| { println!("Peek: {}", x); Ok(()) } );
let to_i32 = Signal::new( |x: &String| Ok(x.parse::
//connect all signals together.
root.lock().unwrap().register_listener(&peek); // snoop on the value! - because we can!
root.lock().unwrap().register_listener(&to_i32); // parse the string to an integer
to_i32.lock().unwrap().register_listener(&inc); //increment the value
inc.lock().unwrap().register_listener(&fail); //then finally fail if the value is 8!
root.lock().unwrap().emit(7); //7 will increment to 8 and fail!
} ```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.