Skip to content

Commit 4a1597f

Browse files
committed
Expressionify LineWriterShim::write
1 parent 0f38158 commit 4a1597f

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/libstd/io/buffered.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -892,45 +892,47 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
892892
/// writer, it will also flush the existing buffer if it contains any
893893
/// newlines, even if the incoming data does not contain any newlines.
894894
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
898898
None => {
899899
// Check for prior partial line writes that need to be retried.
900900
// Only retry if the buffer contains a completed line, to
901901
// avoid flushing partial lines.
902902
if let Some(b'\n') = self.inner.buffer().last().copied() {
903903
self.inner.flush_buf()?;
904904
}
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)
906934
}
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]),
932935
};
933-
Ok(flushed + buffered)
934936
}
935937

936938
fn flush(&mut self) -> io::Result<()> {

0 commit comments

Comments
 (0)