core: Add comm::listen
This commit is contained in:
@@ -34,6 +34,7 @@ export peek;
|
|||||||
export recv_chan;
|
export recv_chan;
|
||||||
export select2;
|
export select2;
|
||||||
export methods;
|
export methods;
|
||||||
|
export listen;
|
||||||
|
|
||||||
|
|
||||||
#[doc = "
|
#[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) {
|
resource port_ptr<T: send>(po: *rust_port) {
|
||||||
// Once the port is detached it's guaranteed not to receive further
|
// Once the port is detached it's guaranteed not to receive further
|
||||||
// messages
|
// messages
|
||||||
@@ -442,3 +449,13 @@ fn test_chan_peek() {
|
|||||||
ch.send(());
|
ch.send(());
|
||||||
assert ch.peek();
|
assert ch.peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_listen() {
|
||||||
|
listen {|parent|
|
||||||
|
task::spawn {||
|
||||||
|
parent.send("oatmeal-salad");
|
||||||
|
}
|
||||||
|
assert parent.recv() == "oatmeal-salad";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user