18
18
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
- use futures:: { prelude:: * , future } ;
21
+ use futures:: prelude:: * ;
22
22
use muxing:: { Shutdown , StreamMuxer } ;
23
23
use std:: io:: { Error as IoError , Read , Write } ;
24
24
use tokio_io:: { AsyncRead , AsyncWrite } ;
25
+ use Multiaddr ;
25
26
26
27
/// Implements `AsyncRead` and `AsyncWrite` and dispatches all method calls to
27
28
/// either `First` or `Second`.
39
40
#[ inline]
40
41
unsafe fn prepare_uninitialized_buffer ( & self , buf : & mut [ u8 ] ) -> bool {
41
42
match self {
42
- & EitherOutput :: First ( ref a) => a. prepare_uninitialized_buffer ( buf) ,
43
- & EitherOutput :: Second ( ref b) => b. prepare_uninitialized_buffer ( buf) ,
43
+ EitherOutput :: First ( a) => a. prepare_uninitialized_buffer ( buf) ,
44
+ EitherOutput :: Second ( b) => b. prepare_uninitialized_buffer ( buf) ,
44
45
}
45
46
}
46
47
}
53
54
#[ inline]
54
55
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , IoError > {
55
56
match self {
56
- & mut EitherOutput :: First ( ref mut a) => a. read ( buf) ,
57
- & mut EitherOutput :: Second ( ref mut b) => b. read ( buf) ,
57
+ EitherOutput :: First ( a) => a. read ( buf) ,
58
+ EitherOutput :: Second ( b) => b. read ( buf) ,
58
59
}
59
60
}
60
61
}
67
68
#[ inline]
68
69
fn shutdown ( & mut self ) -> Poll < ( ) , IoError > {
69
70
match self {
70
- & mut EitherOutput :: First ( ref mut a) => a. shutdown ( ) ,
71
- & mut EitherOutput :: Second ( ref mut b) => b. shutdown ( ) ,
71
+ EitherOutput :: First ( a) => a. shutdown ( ) ,
72
+ EitherOutput :: Second ( b) => b. shutdown ( ) ,
72
73
}
73
74
}
74
75
}
@@ -81,16 +82,16 @@ where
81
82
#[ inline]
82
83
fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , IoError > {
83
84
match self {
84
- & mut EitherOutput :: First ( ref mut a) => a. write ( buf) ,
85
- & mut EitherOutput :: Second ( ref mut b) => b. write ( buf) ,
85
+ EitherOutput :: First ( a) => a. write ( buf) ,
86
+ EitherOutput :: Second ( b) => b. write ( buf) ,
86
87
}
87
88
}
88
89
89
90
#[ inline]
90
91
fn flush ( & mut self ) -> Result < ( ) , IoError > {
91
92
match self {
92
- & mut EitherOutput :: First ( ref mut a) => a. flush ( ) ,
93
- & mut EitherOutput :: Second ( ref mut b) => b. flush ( ) ,
93
+ EitherOutput :: First ( a) => a. flush ( ) ,
94
+ EitherOutput :: Second ( b) => b. flush ( ) ,
94
95
}
95
96
}
96
97
}
@@ -104,16 +105,16 @@ where
104
105
type OutboundSubstream = EitherOutbound < A , B > ;
105
106
106
107
fn poll_inbound ( & self ) -> Poll < Option < Self :: Substream > , IoError > {
107
- match * self {
108
- EitherOutput :: First ( ref inner) => inner. poll_inbound ( ) . map ( |p| p. map ( |o| o. map ( EitherOutput :: First ) ) ) ,
109
- EitherOutput :: Second ( ref inner) => inner. poll_inbound ( ) . map ( |p| p. map ( |o| o. map ( EitherOutput :: Second ) ) ) ,
108
+ match self {
109
+ EitherOutput :: First ( inner) => inner. poll_inbound ( ) . map ( |p| p. map ( |o| o. map ( EitherOutput :: First ) ) ) ,
110
+ EitherOutput :: Second ( inner) => inner. poll_inbound ( ) . map ( |p| p. map ( |o| o. map ( EitherOutput :: Second ) ) ) ,
110
111
}
111
112
}
112
113
113
114
fn open_outbound ( & self ) -> Self :: OutboundSubstream {
114
- match * self {
115
- EitherOutput :: First ( ref inner) => EitherOutbound :: A ( inner. open_outbound ( ) ) ,
116
- EitherOutput :: Second ( ref inner) => EitherOutbound :: B ( inner. open_outbound ( ) ) ,
115
+ match self {
116
+ EitherOutput :: First ( inner) => EitherOutbound :: A ( inner. open_outbound ( ) ) ,
117
+ EitherOutput :: Second ( inner) => EitherOutbound :: B ( inner. open_outbound ( ) ) ,
117
118
}
118
119
}
119
120
@@ -130,14 +131,14 @@ where
130
131
}
131
132
132
133
fn destroy_outbound ( & self , substream : Self :: OutboundSubstream ) {
133
- match * self {
134
- EitherOutput :: First ( ref inner) => {
134
+ match self {
135
+ EitherOutput :: First ( inner) => {
135
136
match substream {
136
137
EitherOutbound :: A ( substream) => inner. destroy_outbound ( substream) ,
137
138
_ => panic ! ( "Wrong API usage" )
138
139
}
139
140
} ,
140
- EitherOutput :: Second ( ref inner) => {
141
+ EitherOutput :: Second ( inner) => {
141
142
match substream {
142
143
EitherOutbound :: B ( substream) => inner. destroy_outbound ( substream) ,
143
144
_ => panic ! ( "Wrong API usage" )
@@ -195,14 +196,14 @@ where
195
196
}
196
197
197
198
fn destroy_substream ( & self , substream : Self :: Substream ) {
198
- match * self {
199
- EitherOutput :: First ( ref inner) => {
199
+ match self {
200
+ EitherOutput :: First ( inner) => {
200
201
match substream {
201
202
EitherOutput :: First ( substream) => inner. destroy_substream ( substream) ,
202
203
_ => panic ! ( "Wrong API usage" )
203
204
}
204
205
} ,
205
- EitherOutput :: Second ( ref inner) => {
206
+ EitherOutput :: Second ( inner) => {
206
207
match substream {
207
208
EitherOutput :: Second ( substream) => inner. destroy_substream ( substream) ,
208
209
_ => panic ! ( "Wrong API usage" )
@@ -212,16 +213,16 @@ where
212
213
}
213
214
214
215
fn shutdown ( & self , kind : Shutdown ) -> Poll < ( ) , IoError > {
215
- match * self {
216
- EitherOutput :: First ( ref inner) => inner. shutdown ( kind) ,
217
- EitherOutput :: Second ( ref inner) => inner. shutdown ( kind)
216
+ match self {
217
+ EitherOutput :: First ( inner) => inner. shutdown ( kind) ,
218
+ EitherOutput :: Second ( inner) => inner. shutdown ( kind)
218
219
}
219
220
}
220
221
221
222
fn flush_all ( & self ) -> Poll < ( ) , IoError > {
222
- match * self {
223
- EitherOutput :: First ( ref inner) => inner. flush_all ( ) ,
224
- EitherOutput :: Second ( ref inner) => inner. flush_all ( )
223
+ match self {
224
+ EitherOutput :: First ( inner) => inner. flush_all ( ) ,
225
+ EitherOutput :: Second ( inner) => inner. flush_all ( )
225
226
}
226
227
}
227
228
}
@@ -243,52 +244,44 @@ pub enum EitherListenStream<A, B> {
243
244
244
245
impl < AStream , BStream , AInner , BInner > Stream for EitherListenStream < AStream , BStream >
245
246
where
246
- AStream : Stream < Item = AInner , Error = IoError > ,
247
- BStream : Stream < Item = BInner , Error = IoError > ,
247
+ AStream : Stream < Item = ( AInner , Multiaddr ) , Error = IoError > ,
248
+ BStream : Stream < Item = ( BInner , Multiaddr ) , Error = IoError > ,
248
249
{
249
- type Item = EitherListenUpgrade < AInner , BInner > ;
250
+ type Item = ( EitherFuture < AInner , BInner > , Multiaddr ) ;
250
251
type Error = IoError ;
251
252
252
253
#[ inline]
253
254
fn poll ( & mut self ) -> Poll < Option < Self :: Item > , Self :: Error > {
254
255
match self {
255
- & mut EitherListenStream :: First ( ref mut a) => a. poll ( )
256
- . map ( |i| i. map ( |v| v. map ( EitherListenUpgrade :: First ) ) ) ,
257
- & mut EitherListenStream :: Second ( ref mut a) => a. poll ( )
258
- . map ( |i| i. map ( |v| v. map ( EitherListenUpgrade :: Second ) ) ) ,
256
+ EitherListenStream :: First ( a) => a. poll ( )
257
+ . map ( |i| ( i. map ( |v| ( v. map ( | ( o , addr ) | ( EitherFuture :: First ( o ) , addr ) ) ) ) ) ) ,
258
+ EitherListenStream :: Second ( a) => a. poll ( )
259
+ . map ( |i| ( i. map ( |v| ( v. map ( | ( o , addr ) | ( EitherFuture :: Second ( o ) , addr ) ) ) ) ) ) ,
259
260
}
260
261
}
261
262
}
262
263
263
- // TODO: This type is needed because of the lack of `impl Trait` in stable Rust.
264
- // If Rust had impl Trait we could use the Either enum from the futures crate and add some
265
- // modifiers to it. This custom enum is a combination of Either and these modifiers.
264
+ /// Implements `Future` and dispatches all method calls to either `First` or `Second`.
266
265
#[ derive( Debug , Copy , Clone ) ]
267
266
#[ must_use = "futures do nothing unless polled" ]
268
- pub enum EitherListenUpgrade < A , B > {
267
+ pub enum EitherFuture < A , B > {
269
268
First ( A ) ,
270
269
Second ( B ) ,
271
270
}
272
271
273
- impl < A , B , Ao , Bo , Af , Bf > Future for EitherListenUpgrade < A , B >
272
+ impl < AFuture , BFuture , AInner , BInner > Future for EitherFuture < AFuture , BFuture >
274
273
where
275
- A : Future < Item = ( Ao , Af ) , Error = IoError > ,
276
- B : Future < Item = ( Bo , Bf ) , Error = IoError > ,
274
+ AFuture : Future < Item = AInner , Error = IoError > ,
275
+ BFuture : Future < Item = BInner , Error = IoError > ,
277
276
{
278
- type Item = ( EitherOutput < Ao , Bo > , future :: Either < Af , Bf > ) ;
277
+ type Item = EitherOutput < AInner , BInner > ;
279
278
type Error = IoError ;
280
279
281
280
#[ inline]
282
281
fn poll ( & mut self ) -> Poll < Self :: Item , Self :: Error > {
283
282
match self {
284
- & mut EitherListenUpgrade :: First ( ref mut a) => {
285
- let ( item, addr) = try_ready ! ( a. poll( ) ) ;
286
- Ok ( Async :: Ready ( ( EitherOutput :: First ( item) , future:: Either :: A ( addr) ) ) )
287
- }
288
- & mut EitherListenUpgrade :: Second ( ref mut b) => {
289
- let ( item, addr) = try_ready ! ( b. poll( ) ) ;
290
- Ok ( Async :: Ready ( ( EitherOutput :: Second ( item) , future:: Either :: B ( addr) ) ) )
291
- }
283
+ EitherFuture :: First ( a) => a. poll ( ) . map ( |v| v. map ( EitherOutput :: First ) ) ,
284
+ EitherFuture :: Second ( a) => a. poll ( ) . map ( |v| v. map ( EitherOutput :: Second ) ) ,
292
285
}
293
286
}
294
287
}
0 commit comments