@@ -171,7 +171,7 @@ use buffer::BufImpl;
171
171
172
172
pub mod policy;
173
173
174
- use self :: policy:: { ReaderPolicy , WriterPolicy , StdPolicy , FlushOnNewline } ;
174
+ use self :: policy:: { FlushOnNewline , ReaderPolicy , StdPolicy , WriterPolicy } ;
175
175
176
176
const DEFAULT_BUF_SIZE : usize = 8 * 1024 ;
177
177
@@ -198,11 +198,15 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024;
198
198
/// [`new_ringbuf()`]: BufReader::new_ringbuf
199
199
/// [`with_capacity_ringbuf()`]: BufReader::with_capacity_ringbuf
200
200
/// [ringbufs-root]: index.html#ringbuffers--slice-deque-feature
201
- pub struct BufReader < R , P = StdPolicy > {
201
+ pub struct BufReader < R , P = StdPolicy > {
202
202
// First field for null pointer optimization.
203
203
buf : Buffer ,
204
+ // We need field "empty" to return empty &[u8] in case of reader is paused
205
+ // This field is never used, never filled.
206
+ empty : Vec < u8 > ,
204
207
inner : R ,
205
208
policy : P ,
209
+ paused : bool ,
206
210
}
207
211
208
212
impl < R > BufReader < R , StdPolicy > {
@@ -262,7 +266,11 @@ impl<R> BufReader<R, StdPolicy> {
262
266
/// then it will be returned in `read()` and `fill_buf()` ahead of any data from `inner`.
263
267
pub fn with_buffer ( buf : Buffer , inner : R ) -> Self {
264
268
BufReader {
265
- buf, inner, policy : StdPolicy
269
+ buf,
270
+ empty : vec ! [ ] ,
271
+ inner,
272
+ policy : StdPolicy ,
273
+ paused : false ,
266
274
}
267
275
}
268
276
}
@@ -272,22 +280,26 @@ impl<R, P> BufReader<R, P> {
272
280
pub fn set_policy < P_ : ReaderPolicy > ( self , policy : P_ ) -> BufReader < R , P_ > {
273
281
BufReader {
274
282
inner : self . inner ,
283
+ empty : self . empty ,
275
284
buf : self . buf ,
276
- policy
285
+ policy,
286
+ paused : self . paused ,
277
287
}
278
288
}
279
289
280
290
/// Mutate the current [`ReaderPolicy`](policy::ReaderPolicy) in-place.
281
291
///
282
292
/// If you want to change the type, use `.set_policy()`.
283
- pub fn policy_mut ( & mut self ) -> & mut P { & mut self . policy }
293
+ pub fn policy_mut ( & mut self ) -> & mut P {
294
+ & mut self . policy
295
+ }
284
296
285
297
/// Inspect the current `ReaderPolicy`.
286
298
pub fn policy ( & self ) -> & P {
287
299
& self . policy
288
300
}
289
301
290
- /// Move data to the start of the buffer, making room at the end for more
302
+ /// Move data to the start of the buffer, making room at the end for more
291
303
/// reading.
292
304
///
293
305
/// This is a no-op with the `*_ringbuf()` constructors (requires `slice-deque` feature).
@@ -321,14 +333,18 @@ impl<R, P> BufReader<R, P> {
321
333
}
322
334
323
335
/// Get an immutable reference to the underlying reader.
324
- pub fn get_ref ( & self ) -> & R { & self . inner }
336
+ pub fn get_ref ( & self ) -> & R {
337
+ & self . inner
338
+ }
325
339
326
340
/// Get a mutable reference to the underlying reader.
327
341
///
328
342
/// ## Note
329
343
/// Reading directly from the underlying reader is not recommended, as some
330
344
/// data has likely already been moved into the buffer.
331
- pub fn get_mut ( & mut self ) -> & mut R { & mut self . inner }
345
+ pub fn get_mut ( & mut self ) -> & mut R {
346
+ & mut self . inner
347
+ }
332
348
333
349
/// Consume `self` and return the inner reader only.
334
350
pub fn into_inner ( self ) -> R {
@@ -357,32 +373,45 @@ impl<R, P: ReaderPolicy> BufReader<R, P> {
357
373
fn should_read ( & mut self ) -> bool {
358
374
self . policy . before_read ( & mut self . buf ) . 0
359
375
}
376
+
377
+ #[ inline]
378
+ fn is_paused ( & mut self ) -> bool {
379
+ self . policy . is_paused ( )
380
+ }
360
381
}
361
382
362
383
impl < R : Read , P > BufReader < R , P > {
363
384
/// Unconditionally perform a read into the buffer.
364
385
///
365
386
/// Does not invoke `ReaderPolicy` methods.
366
- ///
387
+ ///
367
388
/// If the read was successful, returns the number of bytes read.
368
389
pub fn read_into_buf ( & mut self ) -> io:: Result < usize > {
369
390
self . buf . read_from ( & mut self . inner )
370
391
}
371
392
372
393
/// Box the inner reader without losing data.
373
- pub fn boxed < ' a > ( self ) -> BufReader < Box < Read + ' a > , P > where R : ' a {
394
+ pub fn boxed < ' a > ( self ) -> BufReader < Box < Read + ' a > , P >
395
+ where
396
+ R : ' a ,
397
+ {
374
398
let inner: Box < Read + ' a > = Box :: new ( self . inner ) ;
375
-
376
399
BufReader {
377
400
inner,
401
+ empty : self . empty ,
378
402
buf : self . buf ,
379
403
policy : self . policy ,
404
+ paused : self . paused ,
380
405
}
381
406
}
382
407
}
383
408
384
409
impl < R : Read , P : ReaderPolicy > Read for BufReader < R , P > {
385
410
fn read ( & mut self , out : & mut [ u8 ] ) -> io:: Result < usize > {
411
+ // If reading is paused, returning 0 to send end reading signal
412
+ if self . is_paused ( ) {
413
+ return Ok ( 0 ) ;
414
+ }
386
415
// If we don't have any buffered data and we're doing a read matching
387
416
// or exceeding the internal buffer's capacity, bypass the buffer.
388
417
if self . buf . is_empty ( ) && out. len ( ) >= self . buf . capacity ( ) {
@@ -397,12 +426,18 @@ impl<R: Read, P: ReaderPolicy> Read for BufReader<R, P> {
397
426
398
427
impl < R : Read , P : ReaderPolicy > BufRead for BufReader < R , P > {
399
428
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
429
+ // If reading is paused, we are sending empty buffer to send signal - "no data any more"
430
+ if self . is_paused ( ) {
431
+ return Ok ( & self . empty ) ;
432
+ }
400
433
// If we've reached the end of our internal buffer then we need to fetch
401
434
// some more data from the underlying reader.
402
435
// This execution order is important; the policy may want to resize the buffer or move data
403
436
// before reading into it.
404
437
while self . should_read ( ) && self . buf . usable_space ( ) > 0 {
405
- if self . read_into_buf ( ) ? == 0 { break ; } ;
438
+ if self . read_into_buf ( ) ? == 0 {
439
+ break ;
440
+ } ;
406
441
}
407
442
408
443
Ok ( self . buffer ( ) )
@@ -559,7 +594,10 @@ impl<W: Write> BufWriter<W> {
559
594
/// it will be written out on the next flush!
560
595
pub fn with_buffer ( buf : Buffer , inner : W ) -> BufWriter < W > {
561
596
BufWriter {
562
- buf, inner, policy : StdPolicy , panicked : false ,
597
+ buf,
598
+ inner,
599
+ policy : StdPolicy ,
600
+ panicked : false ,
563
601
}
564
602
}
565
603
}
@@ -571,7 +609,10 @@ impl<W: Write, P> BufWriter<W, P> {
571
609
let ( inner, buf) = self . into_inner_ ( ) ;
572
610
573
611
BufWriter {
574
- inner, buf, policy, panicked
612
+ inner,
613
+ buf,
614
+ policy,
615
+ panicked,
575
616
}
576
617
}
577
618
@@ -640,7 +681,9 @@ impl<W: Write, P> BufWriter<W, P> {
640
681
}
641
682
642
683
fn flush_buf ( & mut self , amt : usize ) -> io:: Result < ( ) > {
643
- if amt == 0 || amt > self . buf . len ( ) { return Ok ( ( ) ) }
684
+ if amt == 0 || amt > self . buf . len ( ) {
685
+ return Ok ( ( ) ) ;
686
+ }
644
687
645
688
self . panicked = true ;
646
689
let ret = self . buf . write_max ( amt, & mut self . inner ) ;
@@ -714,7 +757,6 @@ impl<W: Write + fmt::Debug, P: fmt::Debug> fmt::Debug for BufWriter<W, P> {
714
757
}
715
758
}
716
759
717
-
718
760
/// Attempt to flush the buffer to the underlying writer.
719
761
///
720
762
/// If an error occurs, the thread-local handler is invoked, if one was previously
@@ -725,9 +767,7 @@ impl<W: Write, P> Drop for BufWriter<W, P> {
725
767
// instead of ignoring a failed flush, call the handler
726
768
let buf_len = self . buf . len ( ) ;
727
769
if let Err ( err) = self . flush_buf ( buf_len) {
728
- DROP_ERR_HANDLER . with ( |deh| {
729
- ( * deh. borrow ( ) ) ( & mut self . inner , & mut self . buf , err)
730
- } ) ;
770
+ DROP_ERR_HANDLER . with ( |deh| ( * deh. borrow ( ) ) ( & mut self . inner , & mut self . buf , err) ) ;
731
771
}
732
772
}
733
773
}
@@ -805,7 +845,8 @@ impl<W: Write> LineWriter<W> {
805
845
/// Flush the buffer and unwrap, returning the inner writer on success,
806
846
/// or a type wrapping `self` plus the error otherwise.
807
847
pub fn into_inner ( self ) -> Result < W , IntoInnerError < Self > > {
808
- self . 0 . into_inner ( )
848
+ self . 0
849
+ . into_inner ( )
809
850
. map_err ( |IntoInnerError ( inner, e) | IntoInnerError ( LineWriter ( inner) , e) )
810
851
}
811
852
@@ -816,7 +857,7 @@ impl<W: Write> LineWriter<W> {
816
857
}
817
858
818
859
/// Consume `self` and return both the underlying writer and the buffer.
819
- pub fn into_inner_with_buf ( self ) -> ( W , Buffer ) {
860
+ pub fn into_inner_with_buf ( self ) -> ( W , Buffer ) {
820
861
self . 0 . into_inner_with_buffer ( )
821
862
}
822
863
}
@@ -1015,12 +1056,16 @@ impl Buffer {
1015
1056
/// Get an immutable slice of the available bytes in this buffer.
1016
1057
///
1017
1058
/// Call `.consume()` to remove bytes from the beginning of this slice.
1018
- pub fn buf ( & self ) -> & [ u8 ] { self . buf . buf ( ) }
1059
+ pub fn buf ( & self ) -> & [ u8 ] {
1060
+ self . buf . buf ( )
1061
+ }
1019
1062
1020
1063
/// Get a mutable slice representing the available bytes in this buffer.
1021
1064
///
1022
1065
/// Call `.consume()` to remove bytes from the beginning of this slice.
1023
- pub fn buf_mut ( & mut self ) -> & mut [ u8 ] { self . buf . buf_mut ( ) }
1066
+ pub fn buf_mut ( & mut self ) -> & mut [ u8 ] {
1067
+ self . buf . buf_mut ( )
1068
+ }
1024
1069
1025
1070
/// Read from `rdr`, returning the number of bytes read or any errors.
1026
1071
///
@@ -1105,8 +1150,12 @@ impl Buffer {
1105
1150
while self . len ( ) > 0 && max > 0 {
1106
1151
let len = cmp:: min ( self . len ( ) , max) ;
1107
1152
let n = match wrt. write ( & self . buf ( ) [ ..len] ) {
1108
- Ok ( 0 ) => return Err ( io:: Error :: new ( io:: ErrorKind :: WriteZero ,
1109
- "Buffer::write_all() got zero-sized write" ) ) ,
1153
+ Ok ( 0 ) => {
1154
+ return Err ( io:: Error :: new (
1155
+ io:: ErrorKind :: WriteZero ,
1156
+ "Buffer::write_all() got zero-sized write" ,
1157
+ ) )
1158
+ }
1110
1159
Ok ( n) => n,
1111
1160
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: Interrupted => continue ,
1112
1161
Err ( e) => return Err ( e) ,
@@ -1127,8 +1176,12 @@ impl Buffer {
1127
1176
pub fn write_all < W : Write + ?Sized > ( & mut self , wrt : & mut W ) -> io:: Result < ( ) > {
1128
1177
while self . len ( ) > 0 {
1129
1178
match self . write_to ( wrt) {
1130
- Ok ( 0 ) => return Err ( io:: Error :: new ( io:: ErrorKind :: WriteZero ,
1131
- "Buffer::write_all() got zero-sized write" ) ) ,
1179
+ Ok ( 0 ) => {
1180
+ return Err ( io:: Error :: new (
1181
+ io:: ErrorKind :: WriteZero ,
1182
+ "Buffer::write_all() got zero-sized write" ,
1183
+ ) )
1184
+ }
1132
1185
Ok ( _) => ( ) ,
1133
1186
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: Interrupted => ( ) ,
1134
1187
Err ( e) => return Err ( e) ,
@@ -1259,7 +1312,9 @@ pub fn copy_buf<B: BufRead, W: Write>(b: &mut B, w: &mut W) -> io::Result<u64> {
1259
1312
Ok ( buf) => buf,
1260
1313
} ;
1261
1314
1262
- if copied == 0 { break ; }
1315
+ if copied == 0 {
1316
+ break ;
1317
+ }
1263
1318
1264
1319
b. consume ( copied) ;
1265
1320
@@ -1283,7 +1338,8 @@ thread_local!(
1283
1338
/// ### Panics
1284
1339
/// If called from within a handler previously provided to this function.
1285
1340
pub fn set_drop_err_handler < F : ' static > ( handler : F )
1286
- where F : Fn ( & mut Write , & mut Buffer , io:: Error )
1341
+ where
1342
+ F : Fn ( & mut Write , & mut Buffer , io:: Error ) ,
1287
1343
{
1288
1344
DROP_ERR_HANDLER . with ( |deh| * deh. borrow_mut ( ) = Box :: new ( handler) )
1289
1345
}
0 commit comments