@@ -920,6 +920,16 @@ impl<'a, W: Write> LineWriterShim<'a, W> {
920
920
fn buffered ( & self ) -> & [ u8 ] {
921
921
self . buffer . buffer ( )
922
922
}
923
+
924
+ /// Flush the buffer iff the last byte is a newline (indicating that an
925
+ /// earlier write only succeeded partially, and we want to retry flushing
926
+ /// the buffered line before continuing with a subsequent write)
927
+ fn flush_if_completed_line ( & mut self ) -> io:: Result < ( ) > {
928
+ match self . buffered ( ) . last ( ) . copied ( ) {
929
+ Some ( b'\n' ) => self . buffer . flush_buf ( ) ,
930
+ _ => Ok ( ( ) ) ,
931
+ }
932
+ }
923
933
}
924
934
925
935
impl < ' a , W : Write > Write for LineWriterShim < ' a , W > {
@@ -941,12 +951,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
941
951
// If there are no new newlines (that is, if this write is less than
942
952
// one line), just do a regular buffered write
943
953
None => {
944
- // Check for prior partial line writes that need to be retried.
945
- // Only retry if the buffer contains a completed line, to
946
- // avoid flushing partial lines.
947
- if let Some ( b'\n' ) = self . buffered ( ) . last ( ) . copied ( ) {
948
- self . buffer . flush_buf ( ) ?;
949
- }
954
+ self . flush_if_completed_line ( ) ?;
950
955
return self . buffer . write ( buf) ;
951
956
}
952
957
// Otherwise, arrange for the lines to be written directly to the
@@ -1025,9 +1030,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1025
1030
/// Because sorting through an array of `IoSlice` can be a bit convoluted,
1026
1031
/// This method differs from write in the following ways:
1027
1032
///
1028
- /// - It attempts to write all the buffers up to and including the one
1029
- /// containing the last newline. This means that it may attempt to
1030
- /// write a partial line.
1033
+ /// - It attempts to write the full content of all the buffers up to and
1034
+ /// including the one containing the last newline. This means that it
1035
+ /// may attempt to write a partial line, that buffer has data past the
1036
+ /// newline.
1031
1037
/// - If the write only reports partial success, it does not attempt to
1032
1038
/// find the precise location of the written bytes and buffer the rest.
1033
1039
///
@@ -1057,12 +1063,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1057
1063
let last_newline_buf_idx = match last_newline_buf_idx {
1058
1064
// No newlines; just do a normal buffered write
1059
1065
None => {
1060
- // Check for prior partial line writes that need to be retried.
1061
- // Only retry if the buffer contains a completed line, to
1062
- // avoid flushing partial lines.
1063
- if let Some ( b'\n' ) = self . buffered ( ) . last ( ) . copied ( ) {
1064
- self . buffer . flush_buf ( ) ?;
1065
- }
1066
+ self . flush_if_completed_line ( ) ?;
1066
1067
return self . buffer . write_vectored ( bufs) ;
1067
1068
}
1068
1069
Some ( i) => i,
@@ -1109,8 +1110,6 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1109
1110
}
1110
1111
1111
1112
fn is_write_vectored ( & self ) -> bool {
1112
- // It's hard to imagine these diverging, but it's worth checking
1113
- // just in case, because we call `write_vectored` on both.
1114
1113
self . buffer . is_write_vectored ( )
1115
1114
}
1116
1115
@@ -1127,12 +1126,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
1127
1126
// If there are no new newlines (that is, if this write is less than
1128
1127
// one line), just do a regular buffered write
1129
1128
None => {
1130
- // Check for prior partial line writes that need to be retried.
1131
- // Only retry if the buffer contains a completed line, to
1132
- // avoid flushing partial lines.
1133
- if let Some ( b'\n' ) = self . buffered ( ) . last ( ) . copied ( ) {
1134
- self . buffer . flush_buf ( ) ?;
1135
- }
1129
+ self . flush_if_completed_line ( ) ?;
1136
1130
return self . buffer . write_all ( buf) ;
1137
1131
}
1138
1132
// Otherwise, arrange for the lines to be written directly to the
0 commit comments