2012-12-13 13:05:22 -08:00
|
|
|
// xfail-fast
|
|
|
|
|
2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-06 13:42:06 -07:00
|
|
|
|
|
|
|
// An example of the bank protocol from eholk's blog post.
|
|
|
|
//
|
|
|
|
// http://theincredibleholk.wordpress.com/2012/07/06/rusty-pipes/
|
|
|
|
|
2012-09-05 12:32:05 -07:00
|
|
|
use pipes::try_recv;
|
2012-07-06 13:42:06 -07:00
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
pub type username = ~str;
|
|
|
|
pub type password = ~str;
|
|
|
|
pub type money = float;
|
|
|
|
pub type amount = float;
|
2012-07-06 13:42:06 -07:00
|
|
|
|
2012-08-22 18:10:48 -07:00
|
|
|
proto! bank (
|
2012-07-06 13:42:06 -07:00
|
|
|
login:send {
|
2013-01-08 19:37:25 -08:00
|
|
|
login(::username, ::password) -> login_response
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
login_response:recv {
|
|
|
|
ok -> connected,
|
|
|
|
invalid -> login
|
|
|
|
}
|
|
|
|
|
|
|
|
connected:send {
|
2013-01-08 19:37:25 -08:00
|
|
|
deposit(::money) -> connected,
|
|
|
|
withdrawal(::amount) -> withdrawal_response
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
withdrawal_response:recv {
|
2013-01-08 19:37:25 -08:00
|
|
|
money(::money) -> connected,
|
2012-07-06 13:42:06 -07:00
|
|
|
insufficient_funds -> connected
|
|
|
|
}
|
2012-08-22 18:10:48 -07:00
|
|
|
)
|
2012-07-06 13:42:06 -07:00
|
|
|
|
2012-08-22 17:47:11 -07:00
|
|
|
macro_rules! move_it (
|
2012-10-23 11:11:23 -07:00
|
|
|
{ $x:expr } => { unsafe { let y = move *ptr::addr_of(&($x)); move y } }
|
2012-08-22 17:47:11 -07:00
|
|
|
)
|
2012-07-06 13:42:06 -07:00
|
|
|
|
2012-12-11 13:50:04 -08:00
|
|
|
fn switch<T: Owned, U>(+endp: pipes::RecvPacket<T>,
|
2012-09-23 04:39:27 -07:00
|
|
|
f: fn(+v: Option<T>) -> U) -> U {
|
2012-09-18 22:45:24 -07:00
|
|
|
f(pipes::try_recv(move endp))
|
2012-07-18 16:28:58 -07:00
|
|
|
}
|
|
|
|
|
2012-09-18 22:45:24 -07:00
|
|
|
fn move_it<T>(-x: T) -> T { move x }
|
2012-07-18 16:28:58 -07:00
|
|
|
|
2012-08-22 17:47:11 -07:00
|
|
|
macro_rules! follow (
|
2012-08-22 17:24:52 -07:00
|
|
|
{
|
2012-08-06 14:51:53 -07:00
|
|
|
$($message:path$(($($x: ident),+))||* -> $next:ident $e:expr)+
|
2012-07-18 16:28:58 -07:00
|
|
|
} => (
|
2012-08-06 14:51:53 -07:00
|
|
|
|m| match move m {
|
2012-08-20 12:23:37 -07:00
|
|
|
$(Some($message($($($x,)+)* next)) => {
|
2012-08-22 17:24:52 -07:00
|
|
|
let $next = move_it!(next);
|
2012-07-18 16:28:58 -07:00
|
|
|
$e })+
|
2012-08-06 14:51:53 -07:00
|
|
|
_ => { fail }
|
2012-07-18 16:28:58 -07:00
|
|
|
}
|
|
|
|
);
|
2012-08-22 17:47:11 -07:00
|
|
|
)
|
2012-07-18 16:28:58 -07:00
|
|
|
|
|
|
|
fn client_follow(+bank: bank::client::login) {
|
2012-09-07 18:08:21 -07:00
|
|
|
use bank::*;
|
2012-07-18 16:28:58 -07:00
|
|
|
|
2012-09-18 22:45:24 -07:00
|
|
|
let bank = client::login(move bank, ~"theincredibleholk", ~"1234");
|
|
|
|
let bank = switch(move bank, follow! (
|
|
|
|
ok -> connected { move connected }
|
2012-08-06 14:51:53 -07:00
|
|
|
invalid -> _next { fail ~"bank closed the connected" }
|
2012-08-22 17:24:52 -07:00
|
|
|
));
|
2012-07-18 16:28:58 -07:00
|
|
|
|
2012-09-18 22:45:24 -07:00
|
|
|
let bank = client::deposit(move bank, 100.00);
|
|
|
|
let bank = client::withdrawal(move bank, 50.00);
|
|
|
|
switch(move bank, follow! (
|
2012-08-06 14:51:53 -07:00
|
|
|
money(m) -> _next {
|
|
|
|
io::println(~"Yay! I got money!");
|
|
|
|
}
|
|
|
|
insufficient_funds -> _next {
|
|
|
|
fail ~"someone stole my money"
|
|
|
|
}
|
2012-08-22 17:24:52 -07:00
|
|
|
));
|
2012-07-18 16:28:58 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:42:06 -07:00
|
|
|
fn bank_client(+bank: bank::client::login) {
|
2012-09-07 18:08:21 -07:00
|
|
|
use bank::*;
|
2012-07-06 13:42:06 -07:00
|
|
|
|
2012-09-18 22:45:24 -07:00
|
|
|
let bank = client::login(move bank, ~"theincredibleholk", ~"1234");
|
|
|
|
let bank = match try_recv(move bank) {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(ok(connected)) => {
|
2012-08-22 17:24:52 -07:00
|
|
|
move_it!(connected)
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(invalid(_)) => { fail ~"login unsuccessful" }
|
|
|
|
None => { fail ~"bank closed the connection" }
|
2012-07-06 13:42:06 -07:00
|
|
|
};
|
|
|
|
|
2012-09-18 22:45:24 -07:00
|
|
|
let bank = client::deposit(move bank, 100.00);
|
|
|
|
let bank = client::withdrawal(move bank, 50.00);
|
|
|
|
match try_recv(move bank) {
|
|
|
|
Some(money(*)) => {
|
2012-07-13 22:57:48 -07:00
|
|
|
io::println(~"Yay! I got money!");
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(insufficient_funds(_)) => {
|
2012-07-13 22:57:48 -07:00
|
|
|
fail ~"someone stole my money"
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
2012-08-20 12:23:37 -07:00
|
|
|
None => {
|
2012-07-13 22:57:48 -07:00
|
|
|
fail ~"bank closed the connection"
|
2012-07-06 13:42:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2012-07-31 19:27:23 -07:00
|
|
|
}
|