File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ export peek;
34
34
export recv_chan;
35
35
export select2;
36
36
export methods;
37
+ export listen;
37
38
38
39
39
40
#[ doc = "
@@ -86,6 +87,12 @@ impl methods<T: send> for chan<T> {
86
87
87
88
}
88
89
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
+
89
96
resource port_ptr<T : send>( po: * rust_port) {
90
97
// Once the port is detached it's guaranteed not to receive further
91
98
// messages
@@ -441,4 +448,14 @@ fn test_chan_peek() {
441
448
let ch = po. chan ( ) ;
442
449
ch. send ( ( ) ) ;
443
450
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
+ }
444
461
}
You can’t perform that action at this time.
0 commit comments