@@ -203,6 +203,9 @@ pub struct BufReader<R, P = StdPolicy>{
203
203
buf : Buffer ,
204
204
inner : R ,
205
205
policy : P ,
206
+ // We need field "empty" to return empty &[u8] in case of reader is paused
207
+ // This field is never used, never filled.
208
+ empty : Vec < u8 > ,
206
209
}
207
210
208
211
impl < R > BufReader < R , StdPolicy > {
@@ -262,7 +265,7 @@ impl<R> BufReader<R, StdPolicy> {
262
265
/// then it will be returned in `read()` and `fill_buf()` ahead of any data from `inner`.
263
266
pub fn with_buffer ( buf : Buffer , inner : R ) -> Self {
264
267
BufReader {
265
- buf, inner, policy : StdPolicy
268
+ buf, inner, policy : StdPolicy , empty : vec ! [ ]
266
269
}
267
270
}
268
271
}
@@ -273,6 +276,7 @@ impl<R, P> BufReader<R, P> {
273
276
BufReader {
274
277
inner : self . inner ,
275
278
buf : self . buf ,
279
+ empty : self . empty ,
276
280
policy
277
281
}
278
282
}
@@ -357,6 +361,11 @@ impl<R, P: ReaderPolicy> BufReader<R, P> {
357
361
fn should_read ( & mut self ) -> bool {
358
362
self . policy . before_read ( & mut self . buf ) . 0
359
363
}
364
+
365
+ #[ inline]
366
+ fn is_paused ( & mut self ) -> bool {
367
+ self . policy . is_paused ( )
368
+ }
360
369
}
361
370
362
371
impl < R : Read , P > BufReader < R , P > {
@@ -377,12 +386,17 @@ impl<R: Read, P> BufReader<R, P> {
377
386
inner,
378
387
buf : self . buf ,
379
388
policy : self . policy ,
389
+ empty : self . empty ,
380
390
}
381
391
}
382
392
}
383
393
384
394
impl < R : Read , P : ReaderPolicy > Read for BufReader < R , P > {
385
395
fn read ( & mut self , out : & mut [ u8 ] ) -> io:: Result < usize > {
396
+ // If reading is paused, returning 0 to send end reading signal
397
+ if self . is_paused ( ) {
398
+ return Ok ( 0 ) ;
399
+ }
386
400
// If we don't have any buffered data and we're doing a read matching
387
401
// or exceeding the internal buffer's capacity, bypass the buffer.
388
402
if self . buf . is_empty ( ) && out. len ( ) >= self . buf . capacity ( ) {
@@ -397,6 +411,10 @@ impl<R: Read, P: ReaderPolicy> Read for BufReader<R, P> {
397
411
398
412
impl < R : Read , P : ReaderPolicy > BufRead for BufReader < R , P > {
399
413
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
414
+ // If reading is paused, we are sending empty buffer to send signal - "no data any more"
415
+ if self . is_paused ( ) {
416
+ return Ok ( & self . empty ) ;
417
+ }
400
418
// If we've reached the end of our internal buffer then we need to fetch
401
419
// some more data from the underlying reader.
402
420
// This execution order is important; the policy may want to resize the buffer or move data
0 commit comments