Skip to content

LineWriter is broken #148098

@ijackson

Description

@ijackson

Summary

std::io::LineWriter presents the inner writer with partial lines.

The behaviour is bizarre. It is also contrary to a careful reading of the (rather poor) documentation, and makes it unsuitable for most intended use cases.

Steps

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ad71a92706e77761dcefef3658e6b12c

use std::io::{self, LineWriter, Write as _};

struct LineCallWriter(LineWriter<EprintlnDebugWriter>);
impl LineCallWriter {
    pub fn new() -> Self {
        LineCallWriter(LineWriter::new(EprintlnDebugWriter))
    }
}
struct EprintlnDebugWriter;
impl io::Write for LineCallWriter {
    fn write(&mut self, i: &[u8]) -> io::Result<usize> {
        eprintln!("I {:?}", String::from_utf8_lossy(i));
        self.0.write(i)
    }
    fn flush(&mut self) -> io::Result<()> {
        eprintln!("I FLUSH");
        Ok(())
    }
}
impl Drop for LineCallWriter {
    fn drop(&mut self) {
        eprintln!("W DROP");
    }
}
impl io::Write for EprintlnDebugWriter {
    fn write(&mut self, i: &[u8]) -> io::Result<usize> {
        eprintln!("W {:?}", String::from_utf8_lossy(i));
        Ok(i.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        eprintln!("W FLUSH");
        Ok(())
    }
}

fn main() -> Result<(), io::Error> {
    let mut lcw = LineCallWriter::new();
    write!(lcw, "one line at a go, ok\n")?;
    write!(lcw, "in parts, ")?;
    write!(lcw, "bad.\n")?;
    write!(lcw, "multiple lines\nall in one go\n")?;
    write!(lcw, "multiple lines\nwith last one\nsplit, ")?;
    write!(lcw, "wow!\n")?;
    Ok(())
}

Actual output

I "one line at a go, ok\n"
W "one line at a go, ok\n"
I "in parts, "
I "bad.\n"
W "in parts, "
W "bad.\n"
I "multiple lines\nall in one go\n"
W "multiple lines\nall in one go\n"
I "multiple lines\nwith last one\nsplit, "
W "multiple lines\nwith last one\n"
I "wow!\n"
W "split, "
W "wow!\n"
W DROP

Expected output

I "one line at a go, ok\n"
W "one line at a go, ok\n"
I "in parts, "
I "bad.\n"
W "in parts, bad.\n"
I "multiple lines\nall in one go\n"
W "multiple lines\nall in one go\n"
I "multiple lines\nwith last one\nsplit, "
W "multiple lines\nwith last one\n"
I "wow!\n"
W "split, wow!\n"
W DROP

Discussion

The documentation is rather vague, but it does seem to promise that the inner writer will only see whole lines (except at the end). "This batched write" implies a single write to the inner writer.

IMO:

  • Writing multiple lines in one write to the inner writer is perhaps OK. An inner writer that wants to split them can do so easily enough. On the other hand, some use cases would find it more helpful to receive one line at a time. Anyway, the behaviour should be documented.
  • Each write should end with a newline, except during drop and maybe flush. The documentation should promise this explicitly.
  • The behaviour on flush should be documented.

Meta

I saw this with this compiler, but the latest stable on the playground does just the same.

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions