@@ -896,8 +896,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
896
896
// one line), just do a regular buffered write
897
897
let newline_idx = match memchr:: memrchr ( b'\n' , buf) {
898
898
None => {
899
- // Check for prior partial line writes that need to be retried
900
- if memchr:: memchr ( b'\n' , & self . inner . buffer ( ) ) . is_some ( ) {
899
+ // Check for prior partial line writes that need to be retried.
900
+ // Only retry if the buffer contains a completed line, to
901
+ // avoid flushing partial lines.
902
+ if let Some ( b'\n' ) = self . inner . buffer ( ) . last ( ) . copied ( ) {
901
903
self . inner . flush_buf ( ) ?;
902
904
}
903
905
return self . inner . write ( buf) ;
@@ -916,13 +918,13 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
916
918
// `write` convention, make at most one attempt to add new (unbuffered)
917
919
// data. Because this write doesn't touch the BufWriter state directly,
918
920
// and the buffer is known to be empty, we don't need to worry about
919
- // self.panicked here.
921
+ // self.inner. panicked here.
920
922
let flushed = self . inner . get_mut ( ) . write ( lines) ?;
921
923
922
924
// Now that the write has succeeded, buffer the rest (or as much of
923
925
// the rest as possible). If there were any unwritten newlines, we
924
926
// only buffer out to the last unwritten newline; this helps prevent
925
- // flushing partial lines on subsequent calls to write_buffered_lines .
927
+ // flushing partial lines on subsequent calls to LineWriterShim::write .
926
928
let tail = & buf[ flushed..] ;
927
929
let buffered = match memchr:: memrchr ( b'\n' , tail) {
928
930
None => self . inner . write_to_buffer ( tail) ,
@@ -970,8 +972,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
970
972
let last_newline_buf_idx = match last_newline_buf_idx {
971
973
// No newlines; just do a normal buffered write
972
974
None => {
973
- // Check for prior partial line writes that need to be retried
974
- if memchr:: memchr ( b'\n' , & self . inner . buffer ( ) ) . is_some ( ) {
975
+ // Check for prior partial line writes that need to be retried.
976
+ // Only retry if the buffer contains a completed line, to
977
+ // avoid flushing partial lines.
978
+ if let Some ( b'\n' ) = self . inner . buffer ( ) . last ( ) . copied ( ) {
975
979
self . inner . flush_buf ( ) ?;
976
980
}
977
981
return self . inner . write_vectored ( bufs) ;
@@ -1025,8 +1029,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1025
1029
// one line), just do a regular buffered write
1026
1030
let newline_idx = match memchr:: memrchr ( b'\n' , buf) {
1027
1031
None => {
1028
- // Check for prior partial line writes that need to be retried
1029
- if memchr:: memchr ( b'\n' , & self . inner . buffer ( ) ) . is_some ( ) {
1032
+ // Check for prior partial line writes that need to be retried.
1033
+ // Only retry if the buffer contains a completed line, to
1034
+ // avoid flushing partial lines.
1035
+ if let Some ( b'\n' ) = self . inner . buffer ( ) . last ( ) . copied ( ) {
1030
1036
self . inner . flush_buf ( ) ?;
1031
1037
}
1032
1038
return self . inner . write_all ( buf) ;
0 commit comments