@@ -5,17 +5,22 @@ import either::{either, left, right};
5
5
import option:: unwrap;
6
6
import arc:: methods;
7
7
8
- /* Use this after the snapshot
8
+ // Things used by code generated by the pipe compiler.
9
+ export entangle;
10
+
11
+ // User-level things
12
+ export send_packet, recv_packet, send, recv, try_recv, peek;
13
+ export select, select2, selecti, select2i, selectable;
14
+ export spawn_service, spawn_service_recv;
15
+ export stream, port, chan, shared_chan, port_set, channel;
16
+
9
17
macro_rules! move {
10
18
{ $x: expr } => { unsafe { let y <- * ptr:: addr_of( $x) ; y } }
11
19
}
12
- */
13
20
14
- fn macros ( ) {
15
- #macro[
16
- [ #move( x) , { unsafe { let y <- * ptr:: addr_of ( x ) ; y } } ]
17
- ] ;
18
- }
21
+ // This is to help make sure we only move out of enums in safe
22
+ // places. Once there is unary move, it can be removed.
23
+ fn move < T > ( -x : T ) -> T { x }
19
24
20
25
enum state {
21
26
empty,
@@ -465,6 +470,19 @@ proto! streamp {
465
470
}
466
471
}
467
472
473
+ // It'd be nice to call this send, but it'd conflict with the built in
474
+ // send kind.
475
+ trait channel<T : send> {
476
+ fn send( +x: T ) ;
477
+ }
478
+
479
+ trait recv<T : send> {
480
+ fn recv( ) -> T ;
481
+ fn try_recv( ) -> option<T >;
482
+ // This should perhaps be a new trait
483
+ pure fn peek( ) -> bool ;
484
+ }
485
+
468
486
type chan_<T : send> = { mut endp: option<streamp:: client:: open<T >> } ;
469
487
470
488
enum chan<T : send> {
@@ -483,7 +501,7 @@ fn stream<T:send>() -> (chan<T>, port<T>) {
483
501
( chan_ ( { mut endp : some( c) } ) , port_ ( { mut endp : some( s) } ) )
484
502
}
485
503
486
- impl chan<T : send> for chan<T > {
504
+ impl chan<T : send> of channel< T > for chan<T > {
487
505
fn send( +x: T ) {
488
506
let mut endp = none;
489
507
endp <-> self . endp;
@@ -492,7 +510,7 @@ impl chan<T: send> for chan<T> {
492
510
}
493
511
}
494
512
495
- impl port<T : send> for port<T > {
513
+ impl port<T : send> of recv< T > for port<T > {
496
514
fn recv( ) -> T {
497
515
let mut endp = none;
498
516
endp <-> self . endp;
@@ -504,10 +522,10 @@ impl port<T: send> for port<T> {
504
522
fn try_recv( ) -> option<T > {
505
523
let mut endp = none;
506
524
endp <-> self . endp;
507
- alt pipes:: try_recv( unwrap( endp) ) {
525
+ alt move ( pipes:: try_recv( unwrap( endp) ) ) {
508
526
some( streamp:: data( x, endp) ) {
509
- self . endp = some( # move ( endp) ) ;
510
- some( # move ( x ) )
527
+ self . endp = some( move ! { endp} ) ;
528
+ some( move ! { x } )
511
529
}
512
530
none { none }
513
531
}
@@ -528,7 +546,7 @@ impl port<T: send> for port<T> {
528
546
}
529
547
530
548
// Treat a whole bunch of ports as one.
531
- class port_set<T : send> {
549
+ class port_set<T : send> : recv< T > {
532
550
let mut ports: ~[ pipes:: port<T >] ;
533
551
534
552
new( ) { self . ports = ~[ ] ; }
@@ -540,12 +558,12 @@ class port_set<T: send> {
540
558
fn try_recv( ) -> option<T > {
541
559
let mut result = none;
542
560
while result == none && self . ports. len( ) > 0 {
543
- let i = pipes :: wait_many( self . ports. map( |p| p. header( ) ) ) ;
561
+ let i = wait_many( self . ports. map( |p| p. header( ) ) ) ;
544
562
// dereferencing an unsafe pointer nonsense to appease the
545
563
// borrowchecker.
546
- alt unsafe { ( * ptr:: addr_of( self . ports[ i] ) ) . try_recv( ) } {
564
+ alt move ( unsafe { ( * ptr:: addr_of( self . ports[ i] ) ) . try_recv( ) } ) {
547
565
some( m) {
548
- result = some( # move ( m ) ) ;
566
+ result = some( move ! { m } ) ;
549
567
}
550
568
none {
551
569
// Remove this port.
@@ -562,10 +580,19 @@ class port_set<T: send> {
562
580
fn recv( ) -> T {
563
581
option:: unwrap( self . try_recv( ) )
564
582
}
583
+
584
+ pure fn peek( ) -> bool {
585
+ // It'd be nice to use self.port.each, but that version isn't
586
+ // pure.
587
+ for vec:: each( self . ports) |p| {
588
+ if p. peek ( ) { ret true }
589
+ }
590
+ false
591
+ }
565
592
}
566
593
567
- impl <T : send> of selectable for pipes :: port<T > {
568
- pure fn header( ) -> * pipes :: packet_header unchecked {
594
+ impl <T : send> of selectable for port<T > {
595
+ pure fn header( ) -> * packet_header unchecked {
569
596
alt self . endp {
570
597
some( endp) {
571
598
endp. header( )
@@ -576,13 +603,9 @@ impl<T: send> of selectable for pipes::port<T> {
576
603
}
577
604
578
605
579
- type shared_chan<T : send> = arc:: exclusive<pipes:: chan<T >>;
580
-
581
- trait send_on_shared_chan<T > {
582
- fn send( +x: T ) ;
583
- }
606
+ type shared_chan<T : send> = arc:: exclusive<chan<T >>;
584
607
585
- impl chan<T : send> of send_on_shared_chan <T > for shared_chan<T > {
608
+ impl chan<T : send> of channel <T > for shared_chan<T > {
586
609
fn send( +x: T ) {
587
610
let mut xx = some( x) ;
588
611
do self . with |_c, chan| {
@@ -593,6 +616,6 @@ impl chan<T: send> of send_on_shared_chan<T> for shared_chan<T> {
593
616
}
594
617
}
595
618
596
- fn shared_chan<T : send>( +c: pipes :: chan<T >) -> shared_chan<T > {
619
+ fn shared_chan<T : send>( +c: chan<T >) -> shared_chan<T > {
597
620
arc:: exclusive( c)
598
621
}
0 commit comments