Skip to content

Commit 606593f

Browse files
committed
Minor updates
- Remove outdated comment - Refactor flush-retry behavior into its own method - Some other comment updates
1 parent 6a7b5df commit 606593f

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/libstd/io/buffered.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,16 @@ impl<'a, W: Write> LineWriterShim<'a, W> {
920920
fn buffered(&self) -> &[u8] {
921921
self.buffer.buffer()
922922
}
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+
}
923933
}
924934

925935
impl<'a, W: Write> Write for LineWriterShim<'a, W> {
@@ -941,12 +951,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
941951
// If there are no new newlines (that is, if this write is less than
942952
// one line), just do a regular buffered write
943953
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()?;
950955
return self.buffer.write(buf);
951956
}
952957
// Otherwise, arrange for the lines to be written directly to the
@@ -1025,9 +1030,10 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
10251030
/// Because sorting through an array of `IoSlice` can be a bit convoluted,
10261031
/// This method differs from write in the following ways:
10271032
///
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.
10311037
/// - If the write only reports partial success, it does not attempt to
10321038
/// find the precise location of the written bytes and buffer the rest.
10331039
///
@@ -1057,12 +1063,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
10571063
let last_newline_buf_idx = match last_newline_buf_idx {
10581064
// No newlines; just do a normal buffered write
10591065
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()?;
10661067
return self.buffer.write_vectored(bufs);
10671068
}
10681069
Some(i) => i,
@@ -1109,8 +1110,6 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
11091110
}
11101111

11111112
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.
11141113
self.buffer.is_write_vectored()
11151114
}
11161115

@@ -1127,12 +1126,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
11271126
// If there are no new newlines (that is, if this write is less than
11281127
// one line), just do a regular buffered write
11291128
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()?;
11361130
return self.buffer.write_all(buf);
11371131
}
11381132
// Otherwise, arrange for the lines to be written directly to the

0 commit comments

Comments
 (0)