about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-05-02 00:55:40 -0700
committerBrian Anderson <banderson@mozilla.com>2012-05-03 16:38:16 -0700
commitc6d33c3d37c0e9f8db88b7402da45506d7f44d33 (patch)
tree99040c412ba2a41c80473bd77f9470f97e496d48
parent13a4b59cc854d9c47d885bd1e468e094d53756b1 (diff)
downloadrust-c6d33c3d37c0e9f8db88b7402da45506d7f44d33.tar.gz
rust-c6d33c3d37c0e9f8db88b7402da45506d7f44d33.zip
core: Add comm::listen
-rw-r--r--src/libcore/comm.rs17
1 files changed, 17 insertions, 0 deletions
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<T: send> for chan<T> {
 
 }
 
+#[doc = "Open a new receiving channel for the duration of a function"]
+fn listen<T: send, U>(f: fn(chan<T>) -> U) -> U {
+    let po = port();
+    f(po.chan())
+}
+
 resource port_ptr<T: send>(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