Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- run: cargo build
- run: rustdoc --test README.md -L target/debug/deps --extern flate2=target/debug/libflate2.rlib --edition=2018
- run: cargo test
- run: cargo test --features miniz_oxide --no-default-features
- run: cargo test --features zlib
- run: cargo test --features zlib --no-default-features
- run: cargo test --features zlib-default --no-default-features
Expand All @@ -43,6 +44,7 @@ jobs:
- run: cargo test --features zlib-ng --no-default-features
if: matrix.build != 'mingw'
- run: cargo test --features zlib-rs --no-default-features
# The cloudflare_zlib feature flag now forwards to zlib; test that the feature still works.
- run: cargo test --features cloudflare_zlib --no-default-features
if: matrix.build != 'mingw'
- run: |
Expand Down Expand Up @@ -101,7 +103,7 @@ jobs:
matrix:
os: [windows-2022, macos-latest, ubuntu-latest]
env:
version: 1.67.0
version: 1.75.0
steps:
- uses: actions/checkout@v4
- name: Install Rust (rustup)
Expand All @@ -119,12 +121,12 @@ jobs:
- run: cargo build --features cloudflare_zlib --no-default-features

# Minimum Rust compiler to build `flate2` with no optional features
minimum-build:
minimum-build-zlib-rs:
name: Minimum Rust compiler
Comment thread
joshtriplett marked this conversation as resolved.
Resolved
runs-on: ubuntu-latest
env:
# If this is changed to pass tests, then set `rust-version` in `Cargo.toml` to the same version.
version: 1.67.0
version: 1.75.0
steps:
- uses: actions/checkout@v4
- name: Install Rust (rustup)
Expand Down
22 changes: 10 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "1.1.9"
edition = "2018"
license = "MIT OR Apache-2.0"
readme = "README.md"
rust-version = "1.67.0"
rust-version = "1.75.0"

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

rust-version is being bumped to 1.75.0. Per the repository’s MSRV policy in README, when rust-version increases the next release should be a minor version so affected users can pin; please ensure the release/versioning plan reflects this (or document why it’s not needed).

Copilot uses AI. Check for mistakes.
keywords = ["gzip", "deflate", "zlib", "zlib-ng", "encoding"]
categories = ["compression", "api-bindings"]
repository = "https://github.com/rust-lang/flate2-rs"
Expand All @@ -22,37 +22,35 @@ exclude = [".*"]
libz-sys = { version = "1.1.20", optional = true, default-features = false }
libz-ng-sys = { version = "1.1.16", optional = true }
# this matches the default features, but we don't want to depend on the default features staying the same
zlib-rs = { version = "0.6.0", optional = true, default-features = false, features = ["std", "rust-allocator"] }
zlib-rs = { version = "0.6.3", optional = true, default-features = false, features = ["std", "rust-allocator"] }
## This implementation uses only safe Rust code and doesn't require a C compiler.
## It provides good performance for most use cases while being completely portable.
miniz_oxide = { version = "0.8.5", optional = true, default-features = false, features = ["with-alloc", "simd"] }
crc32fast = { version = "1.2.0", optional = true }
document-features = { version = "0.2", optional = true }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
miniz_oxide = { version = "0.8.5", default-features = false, features = ["with-alloc", "simd"] }

[dev-dependencies]
futures = { version = "0.3", default-features = false }
rand = "0.9"
quickcheck = { version = "1.0", default-features = false }

[features]
## The default backend using pure Rust implementation via miniz_oxide.
## This provides a safe, portable compression implementation without requiring a C compiler.
## The default backend using pure Rust implementation via zlib-rs.
## This provides a portable compression implementation without requiring a C compiler.
default = ["rust_backend"]
Comment thread
folkertdev marked this conversation as resolved.
Resolved

#! ### User-Facing Backend Features
#! Choose one of these features to select the compression backend.
#! Only one backend should be enabled at a time, or else one will see an unstable order which is currently
#! `zlib-ng`, `zlib-rs`, `miniz_oxide` and which may change at any time.

## Use the pure Rust `miniz_oxide` backend (default).
## This implementation uses only safe Rust code and doesn't require a C compiler.
## It provides good performance for most use cases while being completely portable.
## Use the pure Rust `zlib-rs` backend (default).
##
## This is the fastest backend overall, providing excellent performance with some `unsafe` code.
## It does not require a C compiler but uses `unsafe` Rust for optimization.
##
## Note that this feature at some point may be switched to use `zlib-rs` instead.
rust_backend = ["miniz_oxide", "any_impl"]
## Previously this feature used miniz_oxide.
rust_backend = ["zlib-rs"]

## Use the zlib-rs backend, a pure Rust rewrite of zlib.
## This is the fastest backend overall, providing excellent performance with some `unsafe` code.
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

A streaming compression/decompression library DEFLATE-based streams in Rust.

This crate by default uses the `miniz_oxide` crate, a port of `miniz.c` to pure
Rust. This crate also supports other [backends](#backends), such as the widely
available zlib library or the high-performance zlib-ng library.
This crate by default uses the `zlib-rs` crate, a pure rust implementation based on zlib-ng.
This crate also supports other [backends](#backends), such as the pure rust `miniz_oxide`,
the widely available zlib C library or the high-performance zlib-ng C library.

Supported formats:

Expand Down Expand Up @@ -67,16 +67,17 @@ fn main() {

## Backends

The default `miniz_oxide` backend has the advantage of only using safe Rust.
The default `zlib-rs` backend brings maximum performance while still benefiting from a Rust
implementation at the cost of some `unsafe`.

If you want maximum performance while still benefiting from a Rust
implementation at the cost of some `unsafe`, you can use `zlib-rs`:
The former default `miniz_oxide` backend has the advantage of only using safe Rust at the cost of some performance.

```toml
[dependencies]
flate2 = { version = "1.0.17", features = ["zlib-rs"], default-features = false }
flate2 = { version = "1.0.17", features = ["miniz_oxide"], default-features = false }
```


### C backends

While zlib-rs is [the fastest overall](https://trifectatech.org/blog/zlib-rs-is-faster-than-c/),
Expand Down
47 changes: 26 additions & 21 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,32 @@ pub trait DeflateBackend: Backend {
fn reset(&mut self);
}

// Default to Rust implementation unless explicitly opted in to a different backend.
#[cfg(feature = "any_c_zlib")]
mod c;
#[cfg(feature = "any_c_zlib")]
pub use self::c::*;

// Only bring in `zlib-rs` if there is no C-based backend.
#[cfg(all(not(feature = "any_c_zlib"), feature = "zlib-rs"))]
mod zlib_rs;
#[cfg(all(not(feature = "any_c_zlib"), feature = "zlib-rs"))]
pub use self::zlib_rs::*;

// Use miniz_oxide when no fully compliant zlib is selected.
#[cfg(all(not(feature = "any_zlib"), feature = "miniz_oxide"))]
mod miniz_oxide;
#[cfg(all(not(feature = "any_zlib"), feature = "miniz_oxide"))]
pub use self::miniz_oxide::*;

// If no backend is enabled, fail fast with a clear error message.
#[cfg(not(feature = "any_impl"))]
compile_error!("No compression backend selected; enable one of `zlib`, `zlib-ng`, `zlib-rs`, or the default `rust_backend` feature.");
// Select the actual implementation that is used.
//
// This choice is straightforward if only one backend is enabled, but the ordering of the branches
// matters when multiple backends are selected (e.g. through feature unification). The top branch
// has the highest priority, the bottom branch the lowest.
crate::cfg_select!(
feature = "any_c_zlib" => {
// Use a C backend when explicitly selected.
mod c;
pub use self::c::*;
}
feature = "miniz_oxide" => {
// Only bring in `miniz_oxide` if there is no C-based backend.
mod miniz_oxide;
pub use self::miniz_oxide::*;
}
feature = "zlib-rs" => {
// Only use `zlib_rs` when no other backend is explicitly selected.
mod zlib_rs;
pub use self::zlib_rs::*;
}
_ => {
// If no backend is enabled, fail fast with a clear error message.
compile_error!("No compression backend selected; enable one of `zlib`, `zlib-ng`, `zlib-rs`, `miniz_oxide`, or the default `rust_backend` feature.");
}
);

impl std::fmt::Debug for ErrorMessage {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand Down
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
//! become active if multiple are selected.
//!
//! * zlib-ng
//! * zlib-rs
//! * miniz_oxide
//! * zlib-rs
//!
//! # Organization
//!
Expand Down Expand Up @@ -251,6 +251,29 @@ impl Default for Compression {
}
}

// While our MSRV does not have stable `cfg_select`, use this manual implementation
// to clean up some of the cfg logic.
macro_rules! cfg_select {
({ $($tt:tt)* }) => {{
$crate::cfg_select! { $($tt)* }
}};
(_ => { $($output:tt)* }) => {
$($output)*
};
(
$cfg:meta => $output:tt
$($( $rest:tt )+)?
) => {
#[cfg($cfg)]
$crate::cfg_select! { _ => $output }
$(
#[cfg(not($cfg))]
$crate::cfg_select! { $($rest)+ }
)?
}
}
pub(crate) use cfg_select;

#[cfg(test)]
fn random_bytes() -> impl Iterator<Item = u8> {
use rand::Rng;
Expand Down
4 changes: 2 additions & 2 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl DecompressError {

impl From<DecompressError> for io::Error {
fn from(data: DecompressError) -> io::Error {
io::Error::new(io::ErrorKind::Other, data)
io::Error::other(data)
}
}

Expand Down Expand Up @@ -618,7 +618,7 @@ impl CompressError {

impl From<CompressError> for io::Error {
fn from(data: CompressError) -> io::Error {
io::Error::new(io::ErrorKind::Other, data)
io::Error::other(data)
}
}

Expand Down
Loading