From c6d33c3d37c0e9f8db88b7402da45506d7f44d33 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 2 May 2012 00:55:40 -0700 Subject: [PATCH] core: Add comm::listen --- src/libcore/comm.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libcore/comm.rs b/src/libcore/comm.rs index b9dc86fd6d6..8ee09a2a646 100644 --- a/src/libcore/comm.rs +++ b/src/libcore/comm.rs @@ -34,6 +34,7 @@ export peek; export recv_chan; export select2; export methods; +export listen; #[doc = " @@ -86,6 +87,12 @@ impl methods for chan { } +#[doc = "Open a new receiving channel for the duration of a function"] +fn listen(f: fn(chan) -> U) -> U { + let po = port(); + f(po.chan()) +} + resource port_ptr(po: *rust_port) { // Once the port is detached it's guaranteed not to receive further // messages @@ -441,4 +448,14 @@ fn test_chan_peek() { let ch = po.chan(); ch.send(()); assert ch.peek(); +} + +#[test] +fn test_listen() { + listen {|parent| + task::spawn {|| + parent.send("oatmeal-salad"); + } + assert parent.recv() == "oatmeal-salad"; + } } \ No newline at end of file