Skip to content

Add Inflate::try_clone, a safe inflateCopy - #562

Open
zone117x wants to merge 1 commit into
trifectatechfoundation:mainfrom
zone117x:inflate-try-clone
Open

zone117x wants to merge 1 commit into
trifectatechfoundation:mainfrom
zone117x:inflate-try-clone

Conversation

@zone117x

@zone117x zone117x commented Sep 6, 2026 •

Copy link
Copy Markdown

The C API has had inflateCopy from the start; the Rust Inflate type had no way to copy a decompressor. This adds Inflate::try_clone, which returns a copy at the current point, from which both continue independently. It is what random access into a deflate stream is built on (zlib's zran example, or a state copy every few MiB): copies saved while the stream is read through, decoding resumed later from the nearest one below the wanted position. We use it for a checkpoint index over gzip in an archive manager, so that a member of a .tar.gz opens in milliseconds rather than after a full decode; until now that meant calling inflateCopy through libz-rs-sys from Rust, since the inflate module sits behind __internal-api.

inflate::copy refused a stream that had not run yet (null next_out). The buffer pointers are copied, never read, so that check is gone; the one inconsistent state, input claimed without a buffer, is still rejected. Two unit tests cover a copy mid-stream and one before the first call.

try_clone rather than Clone, since the state allocation can fail and the crate reports MemError everywhere else.

The C API has had inflateCopy from the start; the Rust `Inflate` type had no way
to copy a decompressor. `try_clone` returns a copy at the current point, from which
both continue independently. It is what random access into a deflate stream is
built on: copies saved while the stream is read through, decoding resumed later
from the nearest one below the wanted position.

`inflate::copy` refused a stream that had not run yet (null buffer pointers). The
pointers are copied, never read, so that check is gone; the one inconsistent
state, input claimed without a buffer, is still rejected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant