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-07-10 13:40:03 -05:00
|
|
|
import 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;
|
|
|
|
|
|
|
|
proto! bank {
|
|
|
|
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-07-18 18:28:58 -05:00
|
|
|
macro_rules! move {
|
|
|
|
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:28:58 -05:00
|
|
|
fn switch<T: send, U>(+endp: pipes::recv_packet<T>,
|
|
|
|
f: fn(+option<T>) -> U) -> U {
|
|
|
|
f(pipes::try_recv(endp))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn move<T>(-x: T) -> T { x }
|
|
|
|
|
|
|
|
macro_rules! follow {
|
|
|
|
{
|
|
|
|
$($message:path($($x: ident),+) => $next:ident $e:expr)+
|
|
|
|
} => (
|
|
|
|
|m| alt move(m) {
|
|
|
|
$(some($message($($x,)* next)) {
|
|
|
|
let $next = move!{next};
|
|
|
|
$e })+
|
|
|
|
_ { fail }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
|
|
|
$($message:path => $next:ident $e:expr)+
|
|
|
|
} => (
|
|
|
|
|m| alt move(m) {
|
|
|
|
$(some($message(next)) {
|
|
|
|
let $next = move!{next};
|
|
|
|
$e })+
|
|
|
|
_ { fail }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fn client_follow(+bank: bank::client::login) {
|
|
|
|
import bank::*;
|
|
|
|
|
|
|
|
let bank = client::login(bank, ~"theincredibleholk", ~"1234");
|
|
|
|
let bank = switch(bank, follow! {
|
|
|
|
ok => connected { connected }
|
|
|
|
invalid => _next { fail ~"bank closed the connected" }
|
|
|
|
});
|
|
|
|
|
|
|
|
/* // potential alternate syntax
|
|
|
|
let bank = recv_alt! {
|
|
|
|
bank => {
|
|
|
|
| ok -> connected { connected }
|
|
|
|
| invalid -> _next { fail }
|
|
|
|
}
|
|
|
|
bank2 => {
|
|
|
|
| foo -> _n { fail }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
let bank = client::deposit(bank, 100.00);
|
|
|
|
let bank = client::withdrawal(bank, 50.00);
|
|
|
|
alt try_recv(bank) {
|
|
|
|
some(money(m, _)) {
|
|
|
|
io::println(~"Yay! I got money!");
|
|
|
|
}
|
|
|
|
some(insufficient_funds(_)) {
|
|
|
|
fail ~"someone stole my money"
|
|
|
|
}
|
|
|
|
none {
|
|
|
|
fail ~"bank closed the connection"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-07-06 15:42:06 -05:00
|
|
|
fn bank_client(+bank: bank::client::login) {
|
|
|
|
import bank::*;
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let bank = client::login(bank, ~"theincredibleholk", ~"1234");
|
2012-07-10 13:40:03 -05:00
|
|
|
let bank = alt try_recv(bank) {
|
2012-07-06 15:42:06 -05:00
|
|
|
some(ok(connected)) {
|
2012-07-18 18:28:58 -05:00
|
|
|
move!{connected}
|
2012-07-06 15:42:06 -05:00
|
|
|
}
|
2012-07-14 00:57:48 -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-07-10 13:40:03 -05:00
|
|
|
alt try_recv(bank) {
|
2012-07-06 15:42:06 -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
|
|
|
}
|
|
|
|
some(insufficient_funds(_)) {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"someone stole my money"
|
2012-07-06 15:42:06 -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() {
|
|
|
|
}
|