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
#![feature(trace_macros)]
use std::sync::mpsc::channel;
use std::thread;
#[macro_export]
macro_rules! funky {
($func: ident) => {{
let (tx, rx) = channel();
thread::spawn(move || {
let result = $func();
tx.send(result).ok().expect("Funky: Could not send result to receiver");
});
rx
}};
($func: ident, $($x: expr),*) => {{
let (tx, rx) = channel();
thread::spawn(move || {
let result = $func($($x),*);
tx.send(result).ok().expect("Funky: Could not send result to receiver");
});
rx
}};
}
#[test]
fn test_simple_function() {
let func = || -> String {
"abc".to_string()
};
let rx = funky!(func);
assert_eq!(rx.recv().unwrap(), "abc".to_string())
}
#[test]
fn test_function_with_argument() {
let func = |string: String| -> String {
string
};
let rx = funky!(func, "abc".to_string());
assert_eq!(rx.recv().unwrap(), "abc".to_string())
}
#[test]
fn test_function_with_multiple_arguments() {
let func = |a: String, b: String| -> String {
(a + &b).to_string()
};
let rx = funky!(func, "a".to_string(), "b".to_string());
assert_eq!(rx.recv().unwrap(), "ab".to_string())
}