@@ -527,7 +527,7 @@ pub pure fn peek<T: Send, Tb: Send>(p: &RecvPacketBuffered<T, Tb>) -> bool {
527
527
}
528
528
}
529
529
530
- impl < T : Send , Tb : Send > RecvPacketBuffered < T , Tb > {
530
+ impl < T : Send , Tb : Send > RecvPacketBuffered < T , Tb > : Peekable < T > {
531
531
pure fn peek ( ) -> bool {
532
532
peek ( & self )
533
533
}
@@ -928,32 +928,32 @@ proto! streamp (
928
928
)
929
929
930
930
/// A trait for things that can send multiple messages.
931
- pub trait Channel < T : Send > {
932
- // It'd be nice to call this send, but it'd conflict with the
933
- // built in send kind.
934
-
931
+ pub trait GenericChan < T > {
935
932
/// Sends a message.
936
933
fn send ( x : T ) ;
934
+ }
937
935
936
+ /// Things that can send multiple messages and can detect when the receiver
937
+ /// is closed
938
+ pub trait GenericSmartChan < T > {
938
939
/// Sends a message, or report if the receiver has closed the connection.
939
940
fn try_send ( x : T ) -> bool ;
940
941
}
941
942
942
943
/// A trait for things that can receive multiple messages.
943
- pub trait Recv < T : Send > {
944
+ pub trait GenericPort < T > {
944
945
/// Receives a message, or fails if the connection closes.
945
946
fn recv ( ) -> T ;
946
947
947
- /** Receives a message if one is available, or returns `none` if
948
- the connection is closed.
949
-
948
+ /** Receives a message, or returns `none` if
949
+ the connection is closed or closes.
950
950
*/
951
951
fn try_recv ( ) -> Option < T > ;
952
+ }
952
953
953
- /** Returns true if a message is available or the connection is
954
- closed.
955
-
956
- */
954
+ /// Ports that can `peek`
955
+ pub trait Peekable < T > {
956
+ /// Returns true if a message is available
957
957
pure fn peek ( ) -> bool ;
958
958
}
959
959
@@ -984,13 +984,16 @@ pub fn stream<T:Send>() -> (Chan<T>, Port<T>) {
984
984
( Chan_ ( { mut endp: Some ( move c) } ) , Port_ ( { mut endp: Some ( move s) } ) )
985
985
}
986
986
987
- impl < T : Send > Chan < T > : Channel < T > {
987
+ impl < T : Send > Chan < T > : GenericChan < T > {
988
988
fn send ( x : T ) {
989
989
let mut endp = None ;
990
990
endp <-> self . endp ;
991
991
self . endp = Some (
992
992
streamp:: client:: data ( unwrap ( move endp) , move x) )
993
993
}
994
+ }
995
+
996
+ impl < T : Send > Chan < T > : GenericSmartChan < T > {
994
997
995
998
fn try_send ( x : T ) -> bool {
996
999
let mut endp = None ;
@@ -1005,7 +1008,7 @@ impl<T: Send> Chan<T>: Channel<T> {
1005
1008
}
1006
1009
}
1007
1010
1008
- impl < T : Send > Port < T > : Recv < T > {
1011
+ impl < T : Send > Port < T > : GenericPort < T > {
1009
1012
fn recv ( ) -> T {
1010
1013
let mut endp = None ;
1011
1014
endp <-> self . endp ;
@@ -1025,7 +1028,9 @@ impl<T: Send> Port<T>: Recv<T> {
1025
1028
None => None
1026
1029
}
1027
1030
}
1031
+ }
1028
1032
1033
+ impl < T : Send > Port < T > : Peekable < T > {
1029
1034
pure fn peek ( ) -> bool unsafe {
1030
1035
let mut endp = None ;
1031
1036
endp <-> self . endp ;
@@ -1071,7 +1076,7 @@ impl<T: Send> PortSet<T> {
1071
1076
}
1072
1077
}
1073
1078
1074
- impl < T : Send > PortSet < T > : Recv < T > {
1079
+ impl < T : Send > PortSet < T > : GenericPort < T > {
1075
1080
1076
1081
fn try_recv ( ) -> Option < T > {
1077
1082
let mut result = None ;
@@ -1099,6 +1104,9 @@ impl<T: Send> PortSet<T> : Recv<T> {
1099
1104
option:: unwrap_expect ( self . try_recv ( ) , "port_set: endpoints closed" )
1100
1105
}
1101
1106
1107
+ }
1108
+
1109
+ impl < T : Send > PortSet < T > : Peekable < T > {
1102
1110
pure fn peek ( ) -> bool {
1103
1111
// It'd be nice to use self.port.each, but that version isn't
1104
1112
// pure.
@@ -1112,7 +1120,7 @@ impl<T: Send> PortSet<T> : Recv<T> {
1112
1120
/// A channel that can be shared between many senders.
1113
1121
pub type SharedChan < T : Send > = private:: Exclusive < Chan < T > > ;
1114
1122
1115
- impl < T : Send > SharedChan < T > : Channel < T > {
1123
+ impl < T : Send > SharedChan < T > : GenericChan < T > {
1116
1124
fn send ( x : T ) {
1117
1125
let mut xx = Some ( move x) ;
1118
1126
do self. with_imm |chan| {
@@ -1121,7 +1129,9 @@ impl<T: Send> SharedChan<T>: Channel<T> {
1121
1129
chan. send ( option:: unwrap ( move x) )
1122
1130
}
1123
1131
}
1132
+ }
1124
1133
1134
+ impl < T : Send > SharedChan < T > : GenericSmartChan < T > {
1125
1135
fn try_send ( x : T ) -> bool {
1126
1136
let mut xx = Some ( move x) ;
1127
1137
do self. with_imm |chan| {
@@ -1145,7 +1155,9 @@ pub trait Select2<T: Send, U: Send> {
1145
1155
fn select ( ) -> Either < T , U > ;
1146
1156
}
1147
1157
1148
- impl < T : Send , U : Send , Left : Selectable Recv < T > , Right : Selectable Recv < U > >
1158
+ impl < T : Send , U : Send ,
1159
+ Left : Selectable GenericPort < T > ,
1160
+ Right : Selectable GenericPort < U > >
1149
1161
( Left , Right ) : Select2 < T , U > {
1150
1162
1151
1163
fn select ( ) -> Either < T , U > {
0 commit comments