1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
extern crate futures;

pub use futures::*;
pub use std::thread;

#[macro_export]
macro_rules! async {
    ($e: expr) => ({
        let (tx, rx) = oneshot();
        thread::spawn(move || {
            tx.complete($e);
        });
        rx
    });
    ($block:block) => ({
        let (tx, rx) = oneshot();
        thread::spawn(move || {
            tx.complete($block);
        });
        rx
    });
}

#[macro_export]
macro_rules! await {
    ($f: expr) => {
        $f.wait().unwrap()
    };
    ($f: expr, $d: expr) => {
        match $f.wait() {
            Ok(e) => e,
            Err(_) => $d
        }
    }
}

#[test]
fn test_simple_async() {
    let a = async!{42};
    assert_eq!(a.wait().unwrap(), 42);
}

#[test]
fn test_complex_async() {
    let f1 = async!{42};
    let f2 = async!{18};
    let transformation = f1.map(|v| v * 2).join((f2.map(|v| v + 5)))
        .and_then(|(v1, v2)| Ok(v1 - v2));
    assert_eq!(61, await!{transformation});
}

#[test]
fn test_block() {
    let f1 = async!{{
        let f1 = async!{42};
        await!{f1.map(|v| v * 2)}
    }};
    assert_eq!(84, await!{f1})
}

#[test]
fn test_await() {
    let a = async!{42};
    assert_eq!(await!(a), 42);
}

#[test]
fn test_default() {
    let a = async!{panic!("i")};
    let res = await!(a, 9711);
    assert_eq!(res, 9711);
}