I’m a beginner with Rust and I’m trying to understand how to properly use the
fn read(&mut self, buf: &mut [u8]) -> Result<usize> method of
For what it’s worth, in my program, this method is implemented by
flate2::bufread::GzDecoder. I’m reading large gz archives, streaming lines one at a time. There are cases when the method returns
Ok(0) before the entire file has been read. I’m having some trouble understanding the contract:
If the return value of this method is Ok(n), then it must be guaranteed that 0 <= n <= buf.len(). A nonzero n value indicates that the buffer buf has been filled in with n bytes of data from this source. If n is 0, then it can indicate one of two scenarios:
- This reader has reached its “end of file” and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
- The buffer specified was 0 bytes in length.
In the first case, it says that the reader could later again be able to produce bytes. How can you tell when that time comes? Do we need to implement some polling? But how can you tell when the reader is no longer able to produce bytes, “for real” this time?