Skip to content

Commit 5edad37

Browse files
committed
Expressionify write_all
1 parent 4a1597f commit 5edad37

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/libstd/io/buffered.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
932932
};
933933
Ok(flushed + buffered)
934934
}
935-
};
935+
}
936936
}
937937

938938
fn flush(&mut self) -> io::Result<()> {
@@ -1027,35 +1027,37 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
10271027
/// writer, it will also flush the existing buffer if it contains any
10281028
/// newlines, even if the incoming data does not contain any newlines.
10291029
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
1030-
// If there are no new newlines (that is, if this write is less than
1031-
// one line), just do a regular buffered write
1032-
let newline_idx = match memchr::memrchr(b'\n', buf) {
1030+
match memchr::memrchr(b'\n', buf) {
1031+
// If there are no new newlines (that is, if this write is less than
1032+
// one line), just do a regular buffered write
10331033
None => {
10341034
// Check for prior partial line writes that need to be retried.
10351035
// Only retry if the buffer contains a completed line, to
10361036
// avoid flushing partial lines.
10371037
if let Some(b'\n') = self.inner.buffer().last().copied() {
10381038
self.inner.flush_buf()?;
10391039
}
1040-
return self.inner.write_all(buf);
1040+
self.inner.write_all(buf)
10411041
}
1042-
Some(i) => i,
1043-
};
1044-
1045-
// Flush existing content to prepare for our write
1046-
self.inner.flush_buf()?;
1042+
// Otherwise, arrange for the lines to be written directly to the
1043+
// inner writer.
1044+
Some(newline_idx) => {
1045+
// Flush existing content to prepare for our write
1046+
self.inner.flush_buf()?;
10471047

1048-
// This is what we're going to try to write directly to the inner
1049-
// writer. The rest will be buffered, if nothing goes wrong.
1050-
let (lines, tail) = buf.split_at(newline_idx + 1);
1048+
// This is what we're going to try to write directly to the inner
1049+
// writer. The rest will be buffered, if nothing goes wrong.
1050+
let (lines, tail) = buf.split_at(newline_idx + 1);
10511051

1052-
// Write `lines` directly to the inner writer, bypassing the buffer.
1053-
self.inner.get_mut().write_all(lines)?;
1052+
// Write `lines` directly to the inner writer, bypassing the buffer.
1053+
self.inner.get_mut().write_all(lines)?;
10541054

1055-
// Now that the write has succeeded, buffer the rest with BufWriter::write_all.
1056-
// This will buffer as much as possible, but continue flushing as
1057-
// necessary if our tail is huge.
1058-
self.inner.write_all(tail)
1055+
// Now that the write has succeeded, buffer the rest with BufWriter::write_all.
1056+
// This will buffer as much as possible, but continue flushing as
1057+
// necessary if our tail is huge.
1058+
self.inner.write_all(tail)
1059+
}
1060+
}
10591061
}
10601062
}
10611063

0 commit comments

Comments
 (0)