Skip to content

Commit e20f63d

Browse files
committed
Bank protocol example from blog post
1 parent b925648 commit e20f63d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/test/run-pass/pipe-bank-proto.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// xfail-pretty
2+
3+
// An example of the bank protocol from eholk's blog post.
4+
//
5+
// http://theincredibleholk.wordpress.com/2012/07/06/rusty-pipes/
6+
7+
import pipes::recv;
8+
9+
type username = str;
10+
type password = str;
11+
type money = float;
12+
type amount = float;
13+
14+
proto! bank {
15+
login:send {
16+
login(username, password) -> login_response
17+
}
18+
19+
login_response:recv {
20+
ok -> connected,
21+
invalid -> login
22+
}
23+
24+
connected:send {
25+
deposit(money) -> connected,
26+
withdrawal(amount) -> withdrawal_response
27+
}
28+
29+
withdrawal_response:recv {
30+
money(money) -> connected,
31+
insufficient_funds -> connected
32+
}
33+
}
34+
35+
fn macros() {
36+
#macro[
37+
[#move[x],
38+
unsafe { let y <- *ptr::addr_of(x); y }]
39+
];
40+
}
41+
42+
fn bank_client(+bank: bank::client::login) {
43+
import bank::*;
44+
45+
let bank = client::login(bank, "theincredibleholk", "1234");
46+
let bank = alt recv(bank) {
47+
some(ok(connected)) {
48+
#move(connected)
49+
}
50+
some(invalid(_)) { fail "login unsuccessful" }
51+
none { fail "bank closed the connection" }
52+
};
53+
54+
let bank = client::deposit(bank, 100.00);
55+
let bank = client::withdrawal(bank, 50.00);
56+
alt recv(bank) {
57+
some(money(m, _)) {
58+
io::println("Yay! I got money!");
59+
}
60+
some(insufficient_funds(_)) {
61+
fail "someone stole my money"
62+
}
63+
none {
64+
fail "bank closed the connection"
65+
}
66+
}
67+
}
68+
69+
fn main() {
70+
}

0 commit comments

Comments
 (0)