From 184da128965d8c833c02feb3f16097e3a1a64417 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 6 Aug 2012 14:51:53 -0700 Subject: [PATCH] Nifty macros for receiving from a protocol. --- src/test/run-pass/pipe-bank-proto.rs | 56 +++++++--------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/src/test/run-pass/pipe-bank-proto.rs b/src/test/run-pass/pipe-bank-proto.rs index a3383a688cb..65ea01f9115 100644 --- a/src/test/run-pass/pipe-bank-proto.rs +++ b/src/test/run-pass/pipe-bank-proto.rs @@ -45,65 +45,37 @@ fn move_it(-x: T) -> T { x } macro_rules! follow { { - $($message:path($($x: ident),+) => $next:ident $e:expr)+ + $($message:path$(($($x: ident),+))||* -> $next:ident $e:expr)+ } => ( - |m| match move_it(m) { - $(some($message($($x,)* next)) { + |m| match move m { + $(some($message($($($x,)+)* next)) => { let $next = move_it!{next}; $e })+ - _ { fail } + _ => { fail } } ); - - { - $($message:path => $next:ident $e:expr)+ - } => ( - |m| match move_it(m) { - $(some($message(next)) { - let $next = move_it!{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" } + 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); - match 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" - } - } + switch(bank, follow! { + money(m) -> _next { + io::println(~"Yay! I got money!"); + } + insufficient_funds -> _next { + fail ~"someone stole my money" + } + }); } -*/ fn bank_client(+bank: bank::client::login) { import bank::*;