forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe-select-macro.rs
112 lines (100 loc) · 2.35 KB
/
pipe-select-macro.rs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// xfail-test
// Protocols
proto! foo {
foo:recv {
do_foo -> foo
}
}
proto! bar {
bar:recv {
do_bar(int) -> barbar,
do_baz(bool) -> bazbar,
}
barbar:send {
rebarbar -> bar,
}
bazbar:send {
rebazbar -> bar
}
}
// select!
macro_rules! select_if {
{
$index:expr,
$count:expr,
$port:path => [
$($message:path$(($($x: ident),+))dont_type_this*
-> $next:ident $e:expr),+
],
$( $ports:path => [
$($messages:path$(($($xs: ident),+))dont_type_this*
-> $nexts:ident $es:expr),+
], )*
} => {
log_syntax!{select_if1};
if $index == $count {
alt move pipes::try_recv($port) {
$(some($message($($($x,)+)* next)) => {
// FIXME (#2329) we really want move out of enum here.
let $next = unsafe { let x <- *ptr::addr_of(next); x };
$e
})+
_ => fail
}
} else {
select_if!{
$index,
$count + 1,
$( $ports => [
$($messages$(($($xs),+))dont_type_this*
-> $nexts $es),+
], )*
}
}
};
{
$index:expr,
$count:expr,
} => {
log_syntax!{select_if2};
fail
}
}
macro_rules! select {
{
$( $port:path => {
$($message:path$(($($x: ident),+))dont_type_this*
-> $next:ident $e:expr),+
} )+
} => {
let index = pipes::selecti([$(($port).header()),+]/_);
log_syntax!{select};
log_syntax!{
select_if!{index, 0, $( $port => [
$($message$(($($x),+))dont_type_this* -> $next $e),+
], )+}
};
select_if!{index, 0, $( $port => [
$($message$(($($x),+))dont_type_this* -> $next $e),+
], )+}
}
}
// Code
fn test(+foo: foo::client::foo, +bar: bar::client::bar) {
select! {
foo => {
foo::do_foo -> _next {
}
}
bar => {
bar::do_bar(x) -> _next {
//debug!("%?", x)
},
do_baz(b) -> _next {
//if b { debug!("true") } else { debug!("false") }
}
}
}
}
fn main() {
}