Skip to content

Commit

Permalink
Expose zlib options on cloudflare-zlib (#230)
Browse files Browse the repository at this point in the history
* Expose zlib options on cloudflare-zlib

See #228 (comment).

* Add an any_zlib feature group
  • Loading branch information
RReverser authored Mar 2, 2020
1 parent 9feca9d commit 5ef8702
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ futures = "0.1"

[features]
default = ["rust_backend"]
zlib = ["libz-sys"]
cloudflare_zlib = ["cloudflare-zlib-sys"]
any_zlib = [] # note: this is not a real user-facing feature
zlib = ["any_zlib", "libz-sys"]
cloudflare_zlib = ["any_zlib", "cloudflare-zlib-sys"]
rust_backend = ["miniz_oxide"]
tokio = ["tokio-io", "futures"]

Expand Down
14 changes: 7 additions & 7 deletions src/ffi/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ impl Default for StreamWrapper {
reserved: 0,
opaque: ptr::null_mut(),
state: ptr::null_mut(),
#[cfg(any(feature = "zlib", feature = "cloudflare_zlib"))]
#[cfg(feature = "any_zlib")]
zalloc,
#[cfg(any(feature = "zlib", feature = "cloudflare_zlib"))]
#[cfg(feature = "any_zlib")]
zfree,
#[cfg(not(any(feature = "zlib", feature = "cloudflare_zlib")))]
#[cfg(not(feature = "any_zlib"))]
zalloc: Some(zalloc),
#[cfg(not(any(feature = "zlib", feature = "cloudflare_zlib")))]
#[cfg(not(feature = "any_zlib"))]
zfree: Some(zfree),
}),
}
Expand Down Expand Up @@ -219,7 +219,7 @@ impl InflateBackend for Inflate {
}
}

#[cfg(any(feature = "zlib", feature = "cloudflare_zlib"))]
#[cfg(feature = "any_zlib")]
fn reset(&mut self, zlib_header: bool) {
let bits = if zlib_header {
MZ_DEFAULT_WINDOW_BITS
Expand All @@ -233,7 +233,7 @@ impl InflateBackend for Inflate {
self.inner.total_in = 0;
}

#[cfg(not(any(feature = "zlib", feature = "cloudflare_zlib")))]
#[cfg(not(feature = "any_zlib"))]
fn reset(&mut self, zlib_header: bool) {
*self = Self::make(zlib_header, MZ_DEFAULT_WINDOW_BITS as u8);
}
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Backend for Deflate {
pub use self::c_backend::*;

/// Miniz specific
#[cfg(not(any(feature = "zlib", feature = "cloudflare_zlib")))]
#[cfg(not(feature = "any_zlib"))]
mod c_backend {
pub use miniz_sys::*;
pub type AllocSize = libc::size_t;
Expand Down
16 changes: 8 additions & 8 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl Compress {
///
/// This constructor is only available when the `zlib` feature is used.
/// Other backends currently do not support custom window bits.
#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
pub fn new_with_window_bits(
level: Compression,
zlib_header: bool,
Expand All @@ -235,7 +235,7 @@ impl Compress {
/// Specifies the compression dictionary to use.
///
/// Returns the Adler-32 checksum of the dictionary.
#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, CompressError> {
let stream = &mut *self.inner.inner.stream_wrapper;
let rc = unsafe {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Compress {
/// the compression of the available input data before changing the
/// compression level. Flushing the stream before calling this method
/// ensures that the function will succeed on the first call.
#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
pub fn set_level(&mut self, level: Compression) -> Result<(), CompressError> {
use libc::c_int;
let stream = &mut *self.inner.inner.stream_wrapper;
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Decompress {
///
/// This constructor is only available when the `zlib` feature is used.
/// Other backends currently do not support custom window bits.
#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
pub fn new_with_window_bits(zlib_header: bool, window_bits: u8) -> Decompress {
Decompress {
inner: Inflate::make(zlib_header, window_bits),
Expand Down Expand Up @@ -439,7 +439,7 @@ impl Decompress {
}

/// Specifies the decompression dictionary to use.
#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, DecompressError> {
let stream = &mut *self.inner.inner.stream_wrapper;
let rc = unsafe {
Expand Down Expand Up @@ -505,7 +505,7 @@ mod tests {
use crate::write;
use crate::{Compression, Decompress, FlushDecompress};

#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
use crate::{Compress, FlushCompress};

#[test]
Expand Down Expand Up @@ -566,7 +566,7 @@ mod tests {
assert!(dst.starts_with(string));
}

#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
#[test]
fn set_dictionary_with_zlib_header() {
let string = "hello, hello!".as_bytes();
Expand Down Expand Up @@ -615,7 +615,7 @@ mod tests {
assert_eq!(&decoded[..decoder.total_out() as usize], string);
}

#[cfg(feature = "zlib")]
#[cfg(feature = "any_zlib")]
#[test]
fn set_dictionary_raw() {
let string = "hello, hello!".as_bytes();
Expand Down

0 comments on commit 5ef8702

Please sign in to comment.