2012-07-06 15:42:06 -05:00
|
|
|
// xfail-pretty
|
|
|
|
|
|
|
|
// An example of the bank protocol from eholk's blog post.
|
|
|
|
//
|
|
|
|
// http://theincredibleholk.wordpress.com/2012/07/06/rusty-pipes/
|
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use pipes::try_recv;
|
2012-07-06 15:42:06 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
type username = ~str;
|
|
|
|
type password = ~str;
|
2012-07-06 15:42:06 -05:00
|
|
|
type money = float;
|
|
|
|
type amount = float;
|
|
|
|
|
2012-08-22 20:10:48 -05:00
|
|
|
proto! bank (
|
2012-07-06 15:42:06 -05:00
|
|
|
login:send {
|
|
|
|
login(username, password) -> login_response
|
|
|
|
}
|
|
|
|
|
|
|
|
login_response:recv {
|
|
|
|
ok -> connected,
|
|
|
|
invalid -> login
|
|
|
|
}
|
|
|
|
|
|
|
|
connected:send {
|
|
|
|
deposit(money) -> connected,
|
|
|
|
withdrawal(amount) -> withdrawal_response
|
|
|
|
}
|
|
|
|
|
|
|
|
withdrawal_response:recv {
|
|
|
|
money(money) -> connected,
|
|
|
|
insufficient_funds -> connected
|
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-07-06 15:42:06 -05:00
|
|
|
|
2012-08-22 19:47:11 -05:00
|
|
|
macro_rules! move_it (
|
2012-10-01 14:47:02 -05:00
|
|
|
{ $x:expr } => { unsafe { let y <- *ptr::addr_of(&($x)); y } }
|
2012-08-22 19:47:11 -05:00
|
|
|
)
|
2012-07-06 15:42:06 -05:00
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn switch<T: Send, U>(+endp: pipes::RecvPacket<T>,
|
2012-09-23 06:39:27 -05:00
|
|
|
f: fn(+v: Option<T>) -> U) -> U {
|
2012-07-18 18:28:58 -05:00
|
|
|
f(pipes::try_recv(endp))
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:27:23 -05:00
|
|
|
fn move_it<T>(-x: T) -> T { x }
|
2012-07-18 18:28:58 -05:00
|
|
|
|
2012-08-22 19:47:11 -05:00
|
|
|
macro_rules! follow (
|
2012-08-22 19:24:52 -05:00
|
|
|
{
|
2012-08-06 16:51:53 -05:00
|
|
|
$($message:path$(($($x: ident),+))||* -> $next:ident $e:expr)+
|
2012-07-18 18:28:58 -05:00
|
|
|
} => (
|
2012-08-06 16:51:53 -05:00
|
|
|
|m| match move m {
|
2012-08-20 14:23:37 -05:00
|
|
|
$(Some($message($($($x,)+)* next)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
let $next = move_it!(next);
|
2012-07-18 18:28:58 -05:00
|
|
|
$e })+
|
2012-08-06 16:51:53 -05:00
|
|
|
_ => { fail }
|
2012-07-18 18:28:58 -05:00
|
|
|
}
|
|
|
|
);
|
2012-08-22 19:47:11 -05:00
|
|
|
)
|
2012-07-18 18:28:58 -05:00
|
|
|
|
|
|
|
fn client_follow(+bank: bank::client::login) {
|
2012-09-07 20:08:21 -05:00
|
|
|
use bank::*;
|
2012-07-18 18:28:58 -05:00
|
|
|
|
|
|
|
let bank = client::login(bank, ~"theincredibleholk", ~"1234");
|
2012-08-22 19:24:52 -05:00
|
|
|
let bank = switch(bank, follow! (
|
2012-08-06 16:51:53 -05:00
|
|
|
ok -> connected { connected }
|
|
|
|
invalid -> _next { fail ~"bank closed the connected" }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-18 18:28:58 -05:00
|
|
|
|
|
|
|
let bank = client::deposit(bank, 100.00);
|
|
|
|
let bank = client::withdrawal(bank, 50.00);
|
2012-08-22 19:24:52 -05:00
|
|
|
switch(bank, follow! (
|
2012-08-06 16:51:53 -05:00
|
|
|
money(m) -> _next {
|
|
|
|
io::println(~"Yay! I got money!");
|
|
|
|
}
|
|
|
|
insufficient_funds -> _next {
|
|
|
|
fail ~"someone stole my money"
|
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-18 18:28:58 -05:00
|
|
|
}
|
|
|
|
|
2012-07-06 15:42:06 -05:00
|
|
|
fn bank_client(+bank: bank::client::login) {
|
2012-09-07 20:08:21 -05:00
|
|
|
use bank::*;
|
2012-07-06 15:42:06 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let bank = client::login(bank, ~"theincredibleholk", ~"1234");
|
2012-08-06 14:34:08 -05:00
|
|
|
let bank = match try_recv(bank) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(ok(connected)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
move_it!(connected)
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(invalid(_)) => { fail ~"login unsuccessful" }
|
|
|
|
None => { fail ~"bank closed the connection" }
|
2012-07-06 15:42:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let bank = client::deposit(bank, 100.00);
|
|
|
|
let bank = client::withdrawal(bank, 50.00);
|
2012-08-06 14:34:08 -05:00
|
|
|
match try_recv(bank) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(money(m, _)) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
io::println(~"Yay! I got money!");
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(insufficient_funds(_)) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"someone stole my money"
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"bank closed the connection"
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2012-07-31 21:27:23 -05:00
|
|
|
}
|