3 ms·
Are you sure about this? AFAIK, println! uses io::stdout(), which is defined like so: pub struct Stdout { inner: Arc<ReentrantMutex<RefCell<LineWri
by burntsushi 8y ago
Are you sure about this? AFAIK, println! uses io::stdout(), which is defined like so:
pub struct Stdout {
inner: Arc<ReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>,
}
In particular, it's specifically using LineWriter internally, which does flush whenever it sees a `\n`.
println! does not itself explicitly flush, but its current implementation does guarantee that it will. However, I don't think this is an API guarantee. In the definition of Stdout, there is this FIXME:
// FIXME: this should be LineWriter or BufWriter depending on the state of
// stdout (tty or not). Note that if this is not line buffered it
// should also flush-on-panic or some form of flush-on-abort.
Which basically means that someone thought its buffering strategy should be determine by whether or not stdout is connected to a tty. This is what ripgrep does for example, but I had to build out my own infrastructure to do it: https://docs.rs/grep-cli/0.1.1/grep_cli/#coloring-and-buffering https://docs.rs/grep-cli/0.1.1/grep_cli/#coloring-and-buffer... --- Arguably, io::stdout() should do the same.
I don't think buffer configuration at the level of the terminal plays a role here. This is somewhat confused by programs like stdbuf[1] which purport to enable users to change the buffering strategy used by a program in their shell. But in reality, it appears tightly coupled with C's FILE data type and perhaps even tightly coupled with glibc itself? I'm not sure.
[1] - https://www.gnu.org/software/coreutils/manual/html_node/stdbuf-invocation.html#stdbuf-invocation https://www.gnu.org/software/coreutils/manual/html_node/stdb...
- steveklabnik 8y agoUgh, you're right. I got it backwards https://github.com/rust-lang/rust/issues/23818 https://github.com/rust-lang/rust/issues/23818 thank you
- DSMan195276 8y ago> I don't think buffer configuration at the level of the terminal plays a role here. I just thought I'd confirm that it does not. The 'line buffering' of `tty`s applies to the input to the `tty` (IE. the keyboard) before it goes to the program - basically, the kernel will buffer characters you typed and not send them to the running program until you hit the `eol` character. On the output side the kernel just handles characters as they come in without any buffering/waiting.
- fanf2 8y agoThanks for those deeper details, enlightening!