core: Add comm::listen

This commit is contained in:
Brian Anderson 2012-05-02 00:55:40 -07:00
parent 13a4b59cc8
commit c6d33c3d37

View File

@ -34,6 +34,7 @@
export recv_chan;
export select2;
export methods;
export listen;
#[doc = "
@ -86,6 +87,12 @@ fn peek() -> bool { peek_chan(self) }
}
#[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";
}
}