From e20f63d095cdafe378821779fcf9f11bc3cfce78 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Fri, 6 Jul 2012 13:42:06 -0700 Subject: [PATCH] Bank protocol example from blog post --- src/test/run-pass/pipe-bank-proto.rs | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/run-pass/pipe-bank-proto.rs diff --git a/src/test/run-pass/pipe-bank-proto.rs b/src/test/run-pass/pipe-bank-proto.rs new file mode 100644 index 000000000000..e6a4a011b30d --- /dev/null +++ b/src/test/run-pass/pipe-bank-proto.rs @@ -0,0 +1,70 @@ +// xfail-pretty + +// An example of the bank protocol from eholk's blog post. +// +// http://theincredibleholk.wordpress.com/2012/07/06/rusty-pipes/ + +import pipes::recv; + +type username = str; +type password = str; +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 + } +} + +fn macros() { + #macro[ + [#move[x], + unsafe { let y <- *ptr::addr_of(x); y }] + ]; +} + +fn bank_client(+bank: bank::client::login) { + import bank::*; + + let bank = client::login(bank, "theincredibleholk", "1234"); + let bank = alt recv(bank) { + some(ok(connected)) { + #move(connected) + } + some(invalid(_)) { fail "login unsuccessful" } + none { fail "bank closed the connection" } + }; + + let bank = client::deposit(bank, 100.00); + let bank = client::withdrawal(bank, 50.00); + alt 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" + } + } +} + +fn main() { +} \ No newline at end of file