18
18
//! [`AsyncRead`]: tokio::io::AsyncRead
19
19
//! [`AsyncWrite`]: tokio::io::AsyncWrite
20
20
21
- use tokio:: io:: { AsyncRead , AsyncWrite } ;
21
+ use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
22
22
use tokio:: sync:: mpsc;
23
23
use tokio:: time:: { self , Delay , Duration , Instant } ;
24
24
@@ -204,20 +204,19 @@ impl Inner {
204
204
self . rx . poll_recv ( cx)
205
205
}
206
206
207
- fn read ( & mut self , dst : & mut [ u8 ] ) -> io:: Result < usize > {
207
+ fn read ( & mut self , dst : & mut ReadBuf < ' _ > ) -> io:: Result < ( ) > {
208
208
match self . action ( ) {
209
209
Some ( & mut Action :: Read ( ref mut data) ) => {
210
210
// Figure out how much to copy
211
- let n = cmp:: min ( dst. len ( ) , data. len ( ) ) ;
211
+ let n = cmp:: min ( dst. remaining ( ) , data. len ( ) ) ;
212
212
213
213
// Copy the data into the `dst` slice
214
- ( & mut dst[ ..n ] ) . copy_from_slice ( & data[ ..n] ) ;
214
+ dst. append ( & data[ ..n] ) ;
215
215
216
216
// Drain the data from the source
217
217
data. drain ( ..n) ;
218
218
219
- // Return the number of bytes read
220
- Ok ( n)
219
+ Ok ( ( ) )
221
220
}
222
221
Some ( & mut Action :: ReadError ( ref mut err) ) => {
223
222
// As the
@@ -229,7 +228,7 @@ impl Inner {
229
228
// Either waiting or expecting a write
230
229
Err ( io:: ErrorKind :: WouldBlock . into ( ) )
231
230
}
232
- None => Ok ( 0 ) ,
231
+ None => Ok ( ( ) ) ,
233
232
}
234
233
}
235
234
@@ -348,8 +347,8 @@ impl AsyncRead for Mock {
348
347
fn poll_read (
349
348
mut self : Pin < & mut Self > ,
350
349
cx : & mut task:: Context < ' _ > ,
351
- buf : & mut [ u8 ] ,
352
- ) -> Poll < io:: Result < usize > > {
350
+ buf : & mut ReadBuf < ' _ > ,
351
+ ) -> Poll < io:: Result < ( ) > > {
353
352
loop {
354
353
if let Some ( ref mut sleep) = self . inner . sleep {
355
354
ready ! ( Pin :: new( sleep) . poll( cx) ) ;
@@ -358,6 +357,9 @@ impl AsyncRead for Mock {
358
357
// If a sleep is set, it has already fired
359
358
self . inner . sleep = None ;
360
359
360
+ // Capture 'filled' to monitor if it changed
361
+ let filled = buf. filled ( ) . len ( ) ;
362
+
361
363
match self . inner . read ( buf) {
362
364
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
363
365
if let Some ( rem) = self . inner . remaining_wait ( ) {
@@ -368,19 +370,22 @@ impl AsyncRead for Mock {
368
370
return Poll :: Pending ;
369
371
}
370
372
}
371
- Ok ( 0 ) => {
372
- // TODO: Extract
373
- match ready ! ( self . inner. poll_action( cx) ) {
374
- Some ( action) => {
375
- self . inner . actions . push_back ( action) ;
376
- continue ;
377
- }
378
- None => {
379
- return Poll :: Ready ( Ok ( 0 ) ) ;
373
+ Ok ( ( ) ) => {
374
+ if buf. filled ( ) . len ( ) == filled {
375
+ match ready ! ( self . inner. poll_action( cx) ) {
376
+ Some ( action) => {
377
+ self . inner . actions . push_back ( action) ;
378
+ continue ;
379
+ }
380
+ None => {
381
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
382
+ }
380
383
}
384
+ } else {
385
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
381
386
}
382
387
}
383
- ret => return Poll :: Ready ( ret ) ,
388
+ Err ( e ) => return Poll :: Ready ( Err ( e ) ) ,
384
389
}
385
390
}
386
391
}
0 commit comments