Skip to content

Commit

Permalink
Better handling of EOF in read_exact_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed May 11, 2024
1 parent e69b4c5 commit 11fceda
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions crates/zune-core/src/bytestream/reader/std_readers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ impl<T: io::BufRead + io::Seek> ZByteReaderTrait for T {
}
#[inline(always)]
fn read_exact_bytes(&mut self, buf: &mut [u8]) -> Result<(), ZByteIoError> {
match self.read(buf) {
Ok(bytes) => {
if bytes != buf.len() {
// if a read succeeds but doesn't satisfy the buffer, it means it may be EOF
// so we seek back to where we started because some paths may aggressively read
// forward and ZCursor maintains the position.
let mut bytes_read = 0;

while bytes_read < buf.len() {
match self.read(&mut buf[bytes_read..]) {
Ok(0) => {
// if a read returns zero bytes read, it means it encountered an EOF so we seek
// back to where we started because some paths may aggressively read forward and
// ZCursor maintains the position.

// NB: (cae) This adds a branch on every read, and will slow down every function
// resting on it. Sorry
self.seek(SeekFrom::Current(-(bytes as i64)))
self.seek(SeekFrom::Current(-(bytes_read as i64)))
.map_err(ZByteIoError::from)?;
return Err(ZByteIoError::NotEnoughBytes(bytes, buf.len()));
return Err(ZByteIoError::NotEnoughBytes(bytes_read, buf.len()));
}
Ok(bytes) => {
bytes_read += bytes;
}
Ok(())
Err(e) => return Err(ZByteIoError::from(e))
}
Err(e) => Err(ZByteIoError::from(e)),
}

Ok(())
}

#[inline]
Expand Down

0 comments on commit 11fceda

Please sign in to comment.