`BufRead`: detect difference between empty pipe and closed pipe

I'm writing a parser that consumes data from a regular file, a named pipe (mkfifo), or StdIn.
When using BufRead::read_line on a normal file, Ok(0) will tell me the file is closed. However the named pipe (and presumably StdIn) also returns Ok(0) when the pipe is empty.
When I delete the named pipe, it continues returning Ok(0).

Is there a way around this? Or do I need to implement a custom BufRead that does what I need?

BufRead::read_line should block until it encounters a newline or EOF. Only if the stream has reached EOF, Ok(0) is returned (an empty line would still return Ok(1), adding \n to the buffer). For Stdin that's the case. Are you sure calling read_line on your named pipe returns Ok(0) if the pipe is not yet closed? What crate are you using for the pipe?

1 Like

No they don't. They only return Ok(0) if the pipe is closed.

Ah, you're right.

I was doing this:

echo "Line" > named_pipe
echo "Line" > named_pipe
echo "Line" > named_pipe

But that opens and closes the named pipe multiple times.

Thanks for correcting me!