Rfcs: BufWriter.write_line to parallel BufReader.read_line

Created on 22 Oct 2017  路  2Comments  路  Source: rust-lang/rfcs

A suggestion to add a write_line(&str) function to the BufWriter trait as a corollary to the BufReader.read_line() function. It would append the contents of the &str followed by 0xA to the underlying Write object.

T-libs

Most helpful comment

The trait is Write, BufWriter is just a struct that implements it.

This is not symmetric: There is a BufRead trait as well as a Read trait, both implemented by BufReader. The reason for this is that BufRead offers useful functionality that is hard to implement by hand: read_line() is hard to implement with traditional IO APIs because without a buffer on your side you have to read one byte at a time, which is of course very slow (if you read more, you have nowhere to put the data after the linebreak).

There is no similar motivation for write_line. You just write your data, it just happens to be terminated with a linebreak.

While BufReader implements very useful functionality that's impossible without a read buffer, BufWriter is only a small performance optimization (to save syscalls, kernel still buffers your writes by default) and nothing more.

The writeln!() macro does what you want (append a newline). And if you wrap your stream in a BufWriter, the write probably gets buffered together with the linebreak in which case the behavior you get is 100% identical to what your proposed write_line function would do.

All 2 comments

The trait is Write, BufWriter is just a struct that implements it.

This is not symmetric: There is a BufRead trait as well as a Read trait, both implemented by BufReader. The reason for this is that BufRead offers useful functionality that is hard to implement by hand: read_line() is hard to implement with traditional IO APIs because without a buffer on your side you have to read one byte at a time, which is of course very slow (if you read more, you have nowhere to put the data after the linebreak).

There is no similar motivation for write_line. You just write your data, it just happens to be terminated with a linebreak.

While BufReader implements very useful functionality that's impossible without a read buffer, BufWriter is only a small performance optimization (to save syscalls, kernel still buffers your writes by default) and nothing more.

The writeln!() macro does what you want (append a newline). And if you wrap your stream in a BufWriter, the write probably gets buffered together with the linebreak in which case the behavior you get is 100% identical to what your proposed write_line function would do.

Doesn't seem this will lead anywhere so I'll close this. Feel free to reraise it on http://internals.rust-lang.org/ if you think it can go somewhere.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mqudsi picture mqudsi  路  3Comments

silversolver1 picture silversolver1  路  3Comments

onelson picture onelson  路  3Comments

Diggsey picture Diggsey  路  3Comments

clarfonthey picture clarfonthey  路  3Comments