@@ -892,45 +892,47 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
892
892
/// writer, it will also flush the existing buffer if it contains any
893
893
/// newlines, even if the incoming data does not contain any newlines.
894
894
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
895
- // If there are no new newlines (that is, if this write is less than
896
- // one line), just do a regular buffered write
897
- let newline_idx = match memchr :: memrchr ( b'\n' , buf ) {
895
+ match memchr :: memrchr ( b'\n' , buf ) {
896
+ // If there are no new newlines (that is, if this write is less than
897
+ // one line), just do a regular buffered write
898
898
None => {
899
899
// Check for prior partial line writes that need to be retried.
900
900
// Only retry if the buffer contains a completed line, to
901
901
// avoid flushing partial lines.
902
902
if let Some ( b'\n' ) = self . inner . buffer ( ) . last ( ) . copied ( ) {
903
903
self . inner . flush_buf ( ) ?;
904
904
}
905
- return self . inner . write ( buf) ;
905
+ self . inner . write ( buf)
906
+ }
907
+ // Otherwise, arrange for the lines to be written directly to the
908
+ // inner writer.
909
+ Some ( newline_idx) => {
910
+ // Flush existing content to prepare for our write
911
+ self . inner . flush_buf ( ) ?;
912
+
913
+ // This is what we're going to try to write directly to the inner
914
+ // writer. The rest will be buffered, if nothing goes wrong.
915
+ let lines = & buf[ ..newline_idx + 1 ] ;
916
+
917
+ // Write `lines` directly to the inner writer. In keeping with the
918
+ // `write` convention, make at most one attempt to add new (unbuffered)
919
+ // data. Because this write doesn't touch the BufWriter state directly,
920
+ // and the buffer is known to be empty, we don't need to worry about
921
+ // self.inner.panicked here.
922
+ let flushed = self . inner . get_mut ( ) . write ( lines) ?;
923
+
924
+ // Now that the write has succeeded, buffer the rest (or as much of
925
+ // the rest as possible). If there were any unwritten newlines, we
926
+ // only buffer out to the last unwritten newline; this helps prevent
927
+ // flushing partial lines on subsequent calls to LineWriterShim::write.
928
+ let tail = & buf[ flushed..] ;
929
+ let buffered = match memchr:: memrchr ( b'\n' , tail) {
930
+ None => self . inner . write_to_buffer ( tail) ,
931
+ Some ( i) => self . inner . write_to_buffer ( & tail[ ..i + 1 ] ) ,
932
+ } ;
933
+ Ok ( flushed + buffered)
906
934
}
907
- Some ( i) => i,
908
- } ;
909
-
910
- // Flush existing content to prepare for our write
911
- self . inner . flush_buf ( ) ?;
912
-
913
- // This is what we're going to try to write directly to the inner
914
- // writer. The rest will be buffered, if nothing goes wrong.
915
- let lines = & buf[ ..newline_idx + 1 ] ;
916
-
917
- // Write `lines` directly to the inner writer. In keeping with the
918
- // `write` convention, make at most one attempt to add new (unbuffered)
919
- // data. Because this write doesn't touch the BufWriter state directly,
920
- // and the buffer is known to be empty, we don't need to worry about
921
- // self.inner.panicked here.
922
- let flushed = self . inner . get_mut ( ) . write ( lines) ?;
923
-
924
- // Now that the write has succeeded, buffer the rest (or as much of
925
- // the rest as possible). If there were any unwritten newlines, we
926
- // only buffer out to the last unwritten newline; this helps prevent
927
- // flushing partial lines on subsequent calls to LineWriterShim::write.
928
- let tail = & buf[ flushed..] ;
929
- let buffered = match memchr:: memrchr ( b'\n' , tail) {
930
- None => self . inner . write_to_buffer ( tail) ,
931
- Some ( i) => self . inner . write_to_buffer ( & tail[ ..i + 1 ] ) ,
932
935
} ;
933
- Ok ( flushed + buffered)
934
936
}
935
937
936
938
fn flush ( & mut self ) -> io:: Result < ( ) > {
0 commit comments