core: Add comm::listen
This commit is contained in:
parent
13a4b59cc8
commit
c6d33c3d37
@ -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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user