Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

Eliminates FFI layer by using zlib-rs directly instead of its C-compatible wrapper libz-rs-sys. The underlying implementation is identical; this removes an unnecessary indirection layer.

Changes

Dependency update (gix-features/Cargo.toml):

  • libz-rs-syszlib-rs

API migration (gix-features/src/zlib/):

  • libz_rs_sys::inflateInit_()zlib_rs::inflate::init()
  • libz_rs_sys::inflate()zlib_rs::inflate::inflate()
  • libz_rs_sys::deflate()zlib_rs::deflate::deflate()
  • Similar changes for reset() and end() functions

Safety boundary:

  • Minimal unsafe remains only for z_stream ↔ typed stream conversions
  • Added Send/Sync impls and safety comments
  • Compression/decompression logic now runs through pure Rust API

Example

// Before: FFI wrapper
unsafe {
    libz_rs_sys::inflateInit_(&mut stream, libz_rs_sys::zlibVersion(), size);
}
unsafe { libz_rs_sys::inflate(&mut stream, flush) }

// After: Direct Rust API
zlib_rs::inflate::init(&mut stream, config);
let stream = unsafe { zlib_rs::inflate::InflateStream::from_stream_mut(&mut stream) };
unsafe { zlib_rs::inflate::inflate(stream, flush) }  // Pure Rust internally

Note: deflate() is fully safe; inflate() remains unsafe in zlib-rs API due to output buffer handling.

Original prompt

Replace libz-rs-sys with zlib-rs, which is the underlying implementation. That way there is no unsafe code related to zlib usage.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Replace libz-rs-sys with zlib-rs implementation Replace libz-rs-sys FFI wrapper with direct zlib-rs usage Dec 19, 2025
Copilot AI requested a review from Byron December 19, 2025 08:26
Copilot AI and others added 2 commits December 19, 2025 11:19
- avoid all unsafe by avoiding the C-api completely
@Byron Byron force-pushed the copilot/replace-libz-rs-sys-with-zlib-rs branch from fd7dc7f to e08b9b5 Compare December 19, 2025 10:40
@Byron Byron marked this pull request as ready for review December 19, 2025 10:40
@Byron Byron requested a review from Copilot December 19, 2025 11:18
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes the zlib integration by migrating from the FFI wrapper libz-rs-sys to the direct Rust API zlib-rs. The change eliminates unnecessary C FFI boundaries while maintaining identical underlying implementation. All unsafe operations related to zlib usage are removed, with the new API handling stream management through pure Rust types (zlib_rs::Deflate and zlib_rs::Inflate).

Key Changes:

  • Dependency upgrade from libz-rs-sys 0.5.2 to zlib-rs 0.5.4
  • Refactored compression/decompression APIs to use direct Rust methods instead of FFI calls
  • Removed manual Drop, Send, and Sync implementations (now auto-derived from Rust types)
  • Added new error variant NeedDict to handle dictionary-required scenarios in decompression

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
gix-features/Cargo.toml Updates dependency from libz-rs-sys to zlib-rs with version bump to 0.5.4
Cargo.lock Reflects the dependency change and version update in the lock file
gix-features/src/zlib/stream/deflate/mod.rs Migrates Compress from FFI-based z_stream to zlib_rs::Deflate; removes unsafe blocks and manual trait impls; adds DataError variant
gix-features/src/zlib/mod.rs Migrates Decompress from FFI-based z_stream to zlib_rs::Inflate; removes unsafe blocks and manual trait impls; adds NeedDict error variant

@Byron Byron enabled auto-merge December 19, 2025 11:44
@Byron Byron merged commit b77744f into main Dec 19, 2025
28 checks passed
@Byron Byron deleted the copilot/replace-libz-rs-sys-with-zlib-rs branch December 19, 2025 12:01
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.

2 participants