Skip to content

Commit c6d33c3

Browse files
committed
core: Add comm::listen
1 parent 13a4b59 commit c6d33c3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcore/comm.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export peek;
3434
export recv_chan;
3535
export select2;
3636
export methods;
37+
export listen;
3738

3839

3940
#[doc = "
@@ -86,6 +87,12 @@ impl methods<T: send> for chan<T> {
8687

8788
}
8889

90+
#[doc = "Open a new receiving channel for the duration of a function"]
91+
fn listen<T: send, U>(f: fn(chan<T>) -> U) -> U {
92+
let po = port();
93+
f(po.chan())
94+
}
95+
8996
resource port_ptr<T: send>(po: *rust_port) {
9097
// Once the port is detached it's guaranteed not to receive further
9198
// messages
@@ -441,4 +448,14 @@ fn test_chan_peek() {
441448
let ch = po.chan();
442449
ch.send(());
443450
assert ch.peek();
451+
}
452+
453+
#[test]
454+
fn test_listen() {
455+
listen {|parent|
456+
task::spawn {||
457+
parent.send("oatmeal-salad");
458+
}
459+
assert parent.recv() == "oatmeal-salad";
460+
}
444461
}

0 commit comments

Comments
 (0)