Determine if BufRead is empty?


#1

Is there a way to determine if a BufRead is empty without attempting to read any bytes?

I’m stuck using an API that requires me to create a reader from a BufRead, and I have to keep reading until I get an custom error type, and then match against the description of the error type to figure out whether the file is at the end or not :frowning:

I’d much prefer to just check whether the mut BufRead I passed a reference to is at the end, in the read loop, and then break.


#2

If I understood your case, https://doc.rust-lang.org/std/io/trait.BufRead.html#tymethod.fill_buf should work - if it returns an empty slice, you’re at EOF.


#3

Oh boy I must be tired. Very first method in the docs :stuck_out_tongue_closed_eyes:

Works perfectly, thank you!