Created
December 3, 2015 20:36
-
-
Save dylanede/7d27ee974795b3cc333d to your computer and use it in GitHub Desktop.
Prototype relm API usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[test] | |
fn simple_test() { | |
// A Mailbox consists of an Address and a Signal. | |
// Values sent into the Address show up as events on the Signal. | |
let input1 = Mailbox::new(0); // The default value of this input is 0. | |
let input2 = Mailbox::new(0); | |
let input3 = Mailbox::new(0); | |
// This test demonstrates updates propagating from a lift2's inputs updating all at once and one at a time. | |
let mapped = lift2( | |
lift2(input1.signal, input3.signal.clone(), |x, y| x + y), // Both this signal | |
lift2(input2.signal, input3.signal, |x, y| x + y), // and this one are triggered by updates to input3 | |
|x, y| x + y); // This signal is triggered by all the inputs | |
let runtime = Runtime::new(mapped, 4); // 4 is the number of worker threads to use | |
// Inputs go in: | |
input1.address.send(1); | |
input2.address.send(2); | |
input3.address.send(1); // input3 update causes all lift2 nodes to recompute | |
// and results come out (default value first): | |
assert_eq!(runtime.next_blocking(), 0); // (0 + 0) + (0 + 0) | |
assert_eq!(runtime.next_blocking(), 1); // (1 + 0) + (0 + 0) | |
assert_eq!(runtime.next_blocking(), 3); // (1 + 0) + (2 + 0) | |
assert_eq!(runtime.next_blocking(), 5); // (1 + 1) + (2 + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment