File tree 2 files changed +58
-8
lines changed 2 files changed +58
-8
lines changed Original file line number Diff line number Diff line change @@ -259,6 +259,31 @@ impl<R: ?Sized + Seek> BufReader<R> {
259
259
}
260
260
}
261
261
262
+ impl < R > BufReader < R >
263
+ where
264
+ Self : Read ,
265
+ {
266
+ #[ inline]
267
+ pub ( crate ) fn read_byte ( & mut self ) -> Option < io:: Result < u8 > > {
268
+ #[ cold]
269
+ fn slow_path < R > ( this : & mut BufReader < R > ) -> Option < io:: Result < u8 > >
270
+ where
271
+ BufReader < R > : Read ,
272
+ {
273
+ use crate :: io:: SpecReadByte ;
274
+
275
+ this. slow_read_byte ( )
276
+ }
277
+
278
+ let mut byte = 0 ;
279
+ if self . buf . consume_with ( 1 , |claimed| byte = claimed[ 0 ] ) {
280
+ return Some ( Ok ( byte) ) ;
281
+ }
282
+
283
+ slow_path ( self )
284
+ }
285
+ }
286
+
262
287
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
263
288
impl < R : ?Sized + Read > Read for BufReader < R > {
264
289
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
@@ -269,10 +294,8 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
269
294
self . discard_buffer ( ) ;
270
295
return self . inner . read ( buf) ;
271
296
}
272
- let nread = {
273
- let mut rem = self . fill_buf ( ) ?;
274
- rem. read ( buf) ?
275
- } ;
297
+ let mut rem = self . fill_buf ( ) ?;
298
+ let nread = rem. read ( buf) ?;
276
299
self . consume ( nread) ;
277
300
Ok ( nread)
278
301
}
Original file line number Diff line number Diff line change @@ -2777,22 +2777,49 @@ pub struct Bytes<R> {
2777
2777
impl < R : Read > Iterator for Bytes < R > {
2778
2778
type Item = Result < u8 > ;
2779
2779
2780
- #[ inline]
2781
2780
fn next ( & mut self ) -> Option < Result < u8 > > {
2781
+ SpecReadByte :: spec_read_byte ( & mut self . inner )
2782
+ }
2783
+
2784
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2785
+ SizeHint :: size_hint ( & self . inner )
2786
+ }
2787
+ }
2788
+
2789
+ trait SpecReadByte {
2790
+ fn spec_read_byte ( & mut self ) -> Option < Result < u8 > > ;
2791
+ fn slow_read_byte ( & mut self ) -> Option < Result < u8 > > ;
2792
+ }
2793
+
2794
+ impl < R > SpecReadByte for R
2795
+ where
2796
+ Self : Read ,
2797
+ {
2798
+ default fn spec_read_byte ( & mut self ) -> Option < Result < u8 > > {
2799
+ self . slow_read_byte ( )
2800
+ }
2801
+
2802
+ #[ inline]
2803
+ fn slow_read_byte ( & mut self ) -> Option < Result < u8 > > {
2782
2804
let mut byte = 0 ;
2783
2805
loop {
2784
- return match self . inner . read ( slice:: from_mut ( & mut byte) ) {
2806
+ return match self . read ( slice:: from_mut ( & mut byte) ) {
2785
2807
Ok ( 0 ) => None ,
2786
2808
Ok ( ..) => Some ( Ok ( byte) ) ,
2787
2809
Err ( ref e) if e. is_interrupted ( ) => continue ,
2788
2810
Err ( e) => Some ( Err ( e) ) ,
2789
2811
} ;
2790
2812
}
2791
2813
}
2814
+ }
2792
2815
2816
+ impl < R > SpecReadByte for BufReader < R >
2817
+ where
2818
+ Self : Read ,
2819
+ {
2793
2820
#[ inline]
2794
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2795
- SizeHint :: size_hint ( & self . inner )
2821
+ fn spec_read_byte ( & mut self ) -> Option < Result < u8 > > {
2822
+ self . read_byte ( )
2796
2823
}
2797
2824
}
2798
2825
You can’t perform that action at this time.
0 commit comments