@@ -565,7 +565,7 @@ impl<W: Write> BufWriter<W> {
565
565
/// data. Writes as much as possible without exceeding capacity. Returns
566
566
/// the number of bytes written.
567
567
#[ inline]
568
- fn write_to_buffer ( & mut self , buf : & [ u8 ] ) -> usize {
568
+ fn write_to_buf ( & mut self , buf : & [ u8 ] ) -> usize {
569
569
let available = self . buf . capacity ( ) - self . buf . len ( ) ;
570
570
let amt_to_buffer = available. min ( buf. len ( ) ) ;
571
571
self . buf . extend_from_slice ( & buf[ ..amt_to_buffer] ) ;
@@ -689,7 +689,7 @@ impl<W: Write> Write for BufWriter<W> {
689
689
self . panicked = false ;
690
690
r
691
691
} else {
692
- Ok ( self . write_to_buffer ( buf) )
692
+ Ok ( self . write_to_buf ( buf) )
693
693
}
694
694
}
695
695
@@ -703,7 +703,7 @@ impl<W: Write> Write for BufWriter<W> {
703
703
self . panicked = false ;
704
704
r
705
705
} else {
706
- self . write_to_buffer ( buf) ;
706
+ self . write_to_buf ( buf) ;
707
707
Ok ( ( ) )
708
708
}
709
709
}
@@ -861,7 +861,7 @@ impl<W> fmt::Display for IntoInnerError<W> {
861
861
/// Private helper struct for implementing the line-buffered writing logic.
862
862
/// This shim temporarily wraps a BufWriter, and uses its internals to
863
863
/// implement a line-buffered writer (specifically by using the internal
864
- /// methods like write_to_buffer and flush_buffer ). In this way, a more
864
+ /// methods like write_to_buf and flush_buf ). In this way, a more
865
865
/// efficient abstraction can be created than one that only had access to
866
866
/// `write` and `flush`, without needlessly duplicating a lot of the
867
867
/// implementation details of BufWriter. This also allows existing
@@ -925,15 +925,15 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
925
925
}
926
926
// Otherwise, arrange for the lines to be written directly to the
927
927
// inner writer.
928
- Some ( newline_idx) => newline_idx,
928
+ Some ( newline_idx) => newline_idx + 1 ,
929
929
} ;
930
930
931
931
// Flush existing content to prepare for our write
932
932
self . buffer . flush_buf ( ) ?;
933
933
934
934
// This is what we're going to try to write directly to the inner
935
935
// writer. The rest will be buffered, if nothing goes wrong.
936
- let lines = & buf[ ..newline_idx + 1 ] ;
936
+ let lines = & buf[ ..newline_idx] ;
937
937
938
938
// Write `lines` directly to the inner writer. In keeping with the
939
939
// `write` convention, make at most one attempt to add new (unbuffered)
@@ -953,11 +953,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
953
953
// the rest as possible). If there were any unwritten newlines, we
954
954
// only buffer out to the last unwritten newline; this helps prevent
955
955
// flushing partial lines on subsequent calls to LineWriterShim::write.
956
- let tail = & buf[ flushed..] ;
957
- let buffered = match memchr:: memrchr ( b'\n' , tail) {
958
- None => self . buffer . write_to_buffer ( tail) ,
959
- Some ( i) => self . buffer . write_to_buffer ( & tail[ ..i + 1 ] ) ,
960
- } ;
956
+ let tail =
957
+ if flushed < newline_idx { & buf[ flushed..newline_idx] } else { & buf[ newline_idx..] } ;
958
+
959
+ let buffered = self . buffer . write_to_buf ( tail) ;
961
960
Ok ( flushed + buffered)
962
961
}
963
962
@@ -1056,7 +1055,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1056
1055
// Now that the write has succeeded, buffer the rest (or as much of the
1057
1056
// rest as possible)
1058
1057
let buffered: usize =
1059
- tail. iter ( ) . map ( |buf| self . buffer . write_to_buffer ( buf) ) . take_while ( |& n| n > 0 ) . sum ( ) ;
1058
+ tail. iter ( ) . map ( |buf| self . buffer . write_to_buf ( buf) ) . take_while ( |& n| n > 0 ) . sum ( ) ;
1060
1059
1061
1060
Ok ( flushed + buffered)
1062
1061
}
@@ -1076,6 +1075,9 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1076
1075
/// writer, it will also flush the existing buffer if it contains any
1077
1076
/// newlines, even if the incoming data does not contain any newlines.
1078
1077
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
1078
+ // The default `write_all` calls `write` in a loop; we can do better
1079
+ // by simply calling self.inner().write_all directly. This avoids
1080
+ // round trips to `self.buffer` in the event of partial writes.
1079
1081
let newline_idx = match memchr:: memrchr ( b'\n' , buf) {
1080
1082
// If there are no new newlines (that is, if this write is less than
1081
1083
// one line), just do a regular buffered write
0 commit comments