Skip to content

Commit

Permalink
feat: rc-zip-tokio: Re-use cursor if it's at the right offset already
Browse files Browse the repository at this point in the history
cf. #61
  • Loading branch information
fasterthanlime committed Mar 16, 2024
1 parent a26f09d commit 84dc0c6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion rc-zip-tokio/src/read_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,41 @@ where
type File = F;

async fn read_zip_with_size(&self, size: u64) -> Result<ArchiveHandle<'_, F>, Error> {
struct CursorState<'a, F: HasCursor + 'a> {
cursor: <F as HasCursor>::Cursor<'a>,
offset: u64,
}
let mut cstate: Option<CursorState<'_, F>> = None;

let mut fsm = ArchiveFsm::new(size);
loop {
if let Some(offset) = fsm.wants_read() {
match self.cursor_at(offset).read(fsm.space()).await {
trace!(%offset, "read_zip_with_size: wants_read, space len = {}", fsm.space().len());

Check warning on line 58 in rc-zip-tokio/src/read_zip.rs

View check run for this annotation

Codecov / codecov/patch

rc-zip-tokio/src/read_zip.rs#L58

Added line #L58 was not covered by tests

let mut cstate_next = match cstate.take() {
Some(cstate) => {
if cstate.offset == offset {
// all good, re-using
cstate
} else {
CursorState {
cursor: self.cursor_at(offset),
offset,
}
}
}
None => CursorState {
cursor: self.cursor_at(offset),
offset,
},
};

match cstate_next.cursor.read(fsm.space()).await {
Ok(read_bytes) => {
cstate_next.offset += read_bytes as u64;
cstate = Some(cstate_next);

trace!(%read_bytes, "read_zip_with_size: read");

Check warning on line 83 in rc-zip-tokio/src/read_zip.rs

View check run for this annotation

Codecov / codecov/patch

rc-zip-tokio/src/read_zip.rs#L83

Added line #L83 was not covered by tests
if read_bytes == 0 {
return Err(Error::IO(io::ErrorKind::UnexpectedEof.into()));
}
Expand Down

0 comments on commit 84dc0c6

Please sign in to comment.