From 7e0390b51336b9737eefb10d1afc14d5e311fb9d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 Nov 2017 14:53:00 -0800 Subject: [PATCH] Switch `Compression` to a struct No need for it to be an enum for matching and otherwise this allows usage of custom compression levels. Closes #112 --- examples/deflatedecoder-bufread.rs | 2 +- examples/deflatedecoder-read.rs | 2 +- examples/deflatedecoder-write.rs | 2 +- examples/deflateencoder-bufread.rs | 2 +- examples/deflateencoder-read.rs | 2 +- examples/deflateencoder-write.rs | 2 +- examples/gzbuilder.rs | 2 +- examples/gzdecoder-bufread.rs | 2 +- examples/gzdecoder-read.rs | 2 +- examples/gzencoder-bufread.rs | 2 +- examples/gzencoder-read.rs | 2 +- examples/gzencoder-write.rs | 2 +- examples/gzmultidecoder-bufread.rs | 2 +- examples/gzmultidecoder-read.rs | 2 +- examples/zlibdecoder-bufread.rs | 2 +- examples/zlibdecoder-read.rs | 2 +- examples/zlibdecoder-write.rs | 2 +- examples/zlibencoder-bufread.rs | 2 +- examples/zlibencoder-read.rs | 2 +- examples/zlibencoder-write.rs | 2 +- src/deflate/bufread.rs | 4 +-- src/deflate/mod.rs | 28 +++++++++---------- src/deflate/read.rs | 4 +-- src/deflate/write.rs | 4 +-- src/gz/bufread.rs | 6 ++--- src/gz/mod.rs | 26 ++++++++---------- src/gz/read.rs | 6 ++--- src/gz/write.rs | 2 +- src/lib.rs | 43 +++++++++++++++++++++--------- src/mem.rs | 4 +-- src/zlib/bufread.rs | 4 +-- src/zlib/mod.rs | 18 ++++++------- src/zlib/read.rs | 4 +-- src/zlib/write.rs | 4 +-- tests/early-flush.rs | 2 +- tests/zero-write.rs | 2 +- 36 files changed, 108 insertions(+), 93 deletions(-) diff --git a/examples/deflatedecoder-bufread.rs b/examples/deflatedecoder-bufread.rs index 117297feb..22e313d6d 100644 --- a/examples/deflatedecoder-bufread.rs +++ b/examples/deflatedecoder-bufread.rs @@ -8,7 +8,7 @@ use flate2::bufread::DeflateDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/deflatedecoder-read.rs b/examples/deflatedecoder-read.rs index 2d2685780..8515c0928 100644 --- a/examples/deflatedecoder-read.rs +++ b/examples/deflatedecoder-read.rs @@ -8,7 +8,7 @@ use flate2::read::DeflateDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/deflatedecoder-write.rs b/examples/deflatedecoder-write.rs index 46791e623..4c1f5d4d0 100644 --- a/examples/deflatedecoder-write.rs +++ b/examples/deflatedecoder-write.rs @@ -8,7 +8,7 @@ use flate2::write::DeflateDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/deflateencoder-bufread.rs b/examples/deflateencoder-bufread.rs index 4ec2ed1c7..abdebbb07 100644 --- a/examples/deflateencoder-bufread.rs +++ b/examples/deflateencoder-bufread.rs @@ -17,7 +17,7 @@ fn main() { fn open_hello_world() -> io::Result> { let f = File::open("examples/hello_world.txt")?; let b = BufReader::new(f); - let mut deflater = DeflateEncoder::new(b, Compression::Fast); + let mut deflater = DeflateEncoder::new(b, Compression::fast()); let mut buffer = Vec::new(); deflater.read_to_end(&mut buffer)?; Ok(buffer) diff --git a/examples/deflateencoder-read.rs b/examples/deflateencoder-read.rs index bf1bcc6a6..c2ea1450d 100644 --- a/examples/deflateencoder-read.rs +++ b/examples/deflateencoder-read.rs @@ -14,7 +14,7 @@ fn main() { fn deflateencoder_read_hello_world() -> io::Result> { let mut ret_vec = [0;100]; let c = b"hello world"; - let mut deflater = DeflateEncoder::new(&c[..], Compression::Fast); + let mut deflater = DeflateEncoder::new(&c[..], Compression::fast()); let count = deflater.read(&mut ret_vec)?; Ok(ret_vec[0..count].to_vec()) } diff --git a/examples/deflateencoder-write.rs b/examples/deflateencoder-write.rs index f511aec4b..f21af5b99 100644 --- a/examples/deflateencoder-write.rs +++ b/examples/deflateencoder-write.rs @@ -6,7 +6,7 @@ use flate2::write::DeflateEncoder; // Vec implements Write to print the compressed bytes of sample string fn main() { - let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); + let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); println!("{:?}", e.finish().unwrap()); } diff --git a/examples/gzbuilder.rs b/examples/gzbuilder.rs index a61a03f4f..dc8f74f3b 100644 --- a/examples/gzbuilder.rs +++ b/examples/gzbuilder.rs @@ -17,7 +17,7 @@ fn sample_builder() -> Result<(), io::Error> { let mut gz = GzBuilder::new() .filename("hello_world.txt") .comment("test file, please delete") - .write(f, Compression::Default); + .write(f, Compression::default()); gz.write(b"hello world")?; gz.finish()?; Ok(()) diff --git a/examples/gzdecoder-bufread.rs b/examples/gzdecoder-bufread.rs index 77f325194..4e728e14d 100644 --- a/examples/gzdecoder-bufread.rs +++ b/examples/gzdecoder-bufread.rs @@ -8,7 +8,7 @@ use flate2::bufread::GzDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = GzEncoder::new(Vec::new(), Compression::Default); + let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/gzdecoder-read.rs b/examples/gzdecoder-read.rs index 843d7bd6a..8d4917457 100644 --- a/examples/gzdecoder-read.rs +++ b/examples/gzdecoder-read.rs @@ -8,7 +8,7 @@ use flate2::read::GzDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = GzEncoder::new(Vec::new(), Compression::Default); + let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/gzencoder-bufread.rs b/examples/gzencoder-bufread.rs index 8b320d813..3d57cd1d5 100644 --- a/examples/gzencoder-bufread.rs +++ b/examples/gzencoder-bufread.rs @@ -17,7 +17,7 @@ fn main() { fn open_hello_world() -> io::Result> { let f = File::open("examples/hello_world.txt")?; let b = BufReader::new(f); - let mut gz = GzEncoder::new(b, Compression::Fast); + let mut gz = GzEncoder::new(b, Compression::fast()); let mut buffer = Vec::new(); gz.read_to_end(&mut buffer)?; Ok(buffer) diff --git a/examples/gzencoder-read.rs b/examples/gzencoder-read.rs index 65dd26bc4..ab0e596be 100644 --- a/examples/gzencoder-read.rs +++ b/examples/gzencoder-read.rs @@ -14,7 +14,7 @@ fn main() { fn gzencoder_read_hello_world() -> io::Result> { let mut ret_vec = [0;100]; let c = b"hello world"; - let mut z = GzEncoder::new(&c[..], Compression::Fast); + let mut z = GzEncoder::new(&c[..], Compression::fast()); let count = z.read(&mut ret_vec)?; Ok(ret_vec[0..count].to_vec()) } diff --git a/examples/gzencoder-write.rs b/examples/gzencoder-write.rs index b423b5241..5cc1d5d1c 100644 --- a/examples/gzencoder-write.rs +++ b/examples/gzencoder-write.rs @@ -6,7 +6,7 @@ use flate2::write::GzEncoder; // Vec implements Write to print the compressed bytes of sample string fn main() { - let mut e = GzEncoder::new(Vec::new(), Compression::Default); + let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); println!("{:?}", e.finish().unwrap()); } diff --git a/examples/gzmultidecoder-bufread.rs b/examples/gzmultidecoder-bufread.rs index d5db3673e..1b2800374 100644 --- a/examples/gzmultidecoder-bufread.rs +++ b/examples/gzmultidecoder-bufread.rs @@ -8,7 +8,7 @@ use flate2::bufread::MultiGzDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = GzEncoder::new(Vec::new(), Compression::Default); + let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/gzmultidecoder-read.rs b/examples/gzmultidecoder-read.rs index 9263292c9..0b66a268b 100644 --- a/examples/gzmultidecoder-read.rs +++ b/examples/gzmultidecoder-read.rs @@ -8,7 +8,7 @@ use flate2::read::MultiGzDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = GzEncoder::new(Vec::new(), Compression::Default); + let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/zlibdecoder-bufread.rs b/examples/zlibdecoder-bufread.rs index 7216f801e..8a1a603bf 100644 --- a/examples/zlibdecoder-bufread.rs +++ b/examples/zlibdecoder-bufread.rs @@ -8,7 +8,7 @@ use flate2::bufread::ZlibDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_bufreader(bytes).unwrap()); diff --git a/examples/zlibdecoder-read.rs b/examples/zlibdecoder-read.rs index ab82413fb..2b5dc8b21 100644 --- a/examples/zlibdecoder-read.rs +++ b/examples/zlibdecoder-read.rs @@ -8,7 +8,7 @@ use flate2::read::ZlibDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/zlibdecoder-write.rs b/examples/zlibdecoder-write.rs index c70ded3c1..fad136a1b 100644 --- a/examples/zlibdecoder-write.rs +++ b/examples/zlibdecoder-write.rs @@ -8,7 +8,7 @@ use flate2::write::ZlibDecoder; // Compress a sample string and print it after transformation. fn main() { - let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); let bytes = e.finish().unwrap(); println!("{}", decode_reader(bytes).unwrap()); diff --git a/examples/zlibencoder-bufread.rs b/examples/zlibencoder-bufread.rs index 39d93611a..8b8d3d084 100644 --- a/examples/zlibencoder-bufread.rs +++ b/examples/zlibencoder-bufread.rs @@ -17,7 +17,7 @@ fn main() { fn open_hello_world() -> io::Result> { let f = File::open("examples/hello_world.txt")?; let b = BufReader::new(f); - let mut z = ZlibEncoder::new(b, Compression::Fast); + let mut z = ZlibEncoder::new(b, Compression::fast()); let mut buffer = Vec::new(); z.read_to_end(&mut buffer)?; Ok(buffer) diff --git a/examples/zlibencoder-read.rs b/examples/zlibencoder-read.rs index 3aef92ca4..622339186 100644 --- a/examples/zlibencoder-read.rs +++ b/examples/zlibencoder-read.rs @@ -14,7 +14,7 @@ fn main() { // File implements Read fn open_hello_world() -> std::io::Result> { let f = File::open("examples/hello_world.txt")?; - let mut z = ZlibEncoder::new(f, Compression::Fast); + let mut z = ZlibEncoder::new(f, Compression::fast()); let mut buffer = [0;50]; let byte_count = z.read(&mut buffer)?; Ok(buffer[0..byte_count].to_vec()) diff --git a/examples/zlibencoder-write.rs b/examples/zlibencoder-write.rs index 9152b30ea..127bbc64f 100644 --- a/examples/zlibencoder-write.rs +++ b/examples/zlibencoder-write.rs @@ -6,7 +6,7 @@ use flate2::write::ZlibEncoder; // Vec implements Write to print the compressed bytes of sample string fn main() { - let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); + let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); e.write(b"Hello World").unwrap(); println!("{:?}", e.finish().unwrap()); } diff --git a/src/deflate/bufread.rs b/src/deflate/bufread.rs index 9a9a47ec6..b616ab3fd 100644 --- a/src/deflate/bufread.rs +++ b/src/deflate/bufread.rs @@ -35,7 +35,7 @@ use {Compress, Decompress}; /// fn open_hello_world() -> io::Result> { /// let f = File::open("examples/hello_world.txt")?; /// let b = BufReader::new(f); -/// let mut deflater = DeflateEncoder::new(b, Compression::Fast); +/// let mut deflater = DeflateEncoder::new(b, Compression::fast()); /// let mut buffer = Vec::new(); /// deflater.read_to_end(&mut buffer)?; /// Ok(buffer) @@ -154,7 +154,7 @@ impl AsyncWrite for DeflateEncoder { /// use flate2::bufread::DeflateDecoder; /// /// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs index 7bacfa7c8..c3a83963c 100644 --- a/src/deflate/mod.rs +++ b/src/deflate/mod.rs @@ -9,12 +9,12 @@ mod tests { use rand::{thread_rng, Rng}; use super::{read, write}; - use Compression::Default; + use Compression; #[test] fn roundtrip() { let mut real = Vec::new(); - let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); let v = thread_rng().gen_iter::().take(1024).collect::>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0, v.len())]; @@ -31,7 +31,7 @@ mod tests { #[test] fn drop_writes() { let mut data = Vec::new(); - write::DeflateEncoder::new(&mut data, Default) + write::DeflateEncoder::new(&mut data, Compression::default()) .write_all(b"foo") .unwrap(); let mut r = read::DeflateDecoder::new(&data[..]); @@ -43,7 +43,7 @@ mod tests { #[test] fn total_in() { let mut real = Vec::new(); - let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); let v = thread_rng().gen_iter::().take(1024).collect::>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0, v.len())]; @@ -71,7 +71,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Default)); + let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Compression::default())); let mut ret = Vec::new(); r.read_to_end(&mut ret).unwrap(); assert_eq!(ret, v); @@ -83,7 +83,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Default); + let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Compression::default()); w.write_all(&v).unwrap(); let w = w.finish().unwrap().finish().unwrap(); assert!(w == v); @@ -95,13 +95,13 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); w.write_all(&v).unwrap(); let a = w.reset(Vec::new()).unwrap(); w.write_all(&v).unwrap(); let b = w.finish().unwrap(); - let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); w.write_all(&v).unwrap(); let c = w.finish().unwrap(); assert!(a == b && b == c); @@ -114,12 +114,12 @@ mod tests { .take(1024 * 1024) .collect::>(); let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = read::DeflateEncoder::new(&v[..], Default); + let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); r.read_to_end(&mut a).unwrap(); r.reset(&v[..]); r.read_to_end(&mut b).unwrap(); - let mut r = read::DeflateEncoder::new(&v[..], Default); + let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); r.read_to_end(&mut c).unwrap(); assert!(a == b && b == c); } @@ -130,7 +130,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); w.write_all(&v).unwrap(); let data = w.finish().unwrap(); @@ -163,7 +163,7 @@ mod tests { #[test] fn zero_length_read_with_data() { let m = vec![3u8; 128 * 1024 + 1]; - let mut c = read::DeflateEncoder::new(&m[..], ::Compression::Default); + let mut c = read::DeflateEncoder::new(&m[..], Compression::default()); let mut result = Vec::new(); c.read_to_end(&mut result).unwrap(); @@ -178,7 +178,7 @@ mod tests { ::quickcheck::quickcheck(test as fn(_) -> _); fn test(v: Vec) -> bool { - let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Default)); + let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Compression::default())); let mut v2 = Vec::new(); r.read_to_end(&mut v2).unwrap(); v == v2 @@ -190,7 +190,7 @@ mod tests { ::quickcheck::quickcheck(test as fn(_) -> _); fn test(v: Vec) -> bool { - let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Default); + let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Compression::default()); w.write_all(&v).unwrap(); v == w.finish().unwrap().finish().unwrap() } diff --git a/src/deflate/read.rs b/src/deflate/read.rs index e2363c9cc..3b1ef409c 100644 --- a/src/deflate/read.rs +++ b/src/deflate/read.rs @@ -32,7 +32,7 @@ use super::bufread; /// fn deflateencoder_read_hello_world() -> io::Result> { /// let mut ret_vec = [0;100]; /// let c = b"hello world"; -/// let mut deflater = DeflateEncoder::new(&c[..], Compression::Fast); +/// let mut deflater = DeflateEncoder::new(&c[..], Compression::fast()); /// let count = deflater.read(&mut ret_vec)?; /// Ok(ret_vec[0..count].to_vec()) /// } @@ -150,7 +150,7 @@ impl AsyncWrite for DeflateEncoder { /// use flate2::read::DeflateDecoder; /// /// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/src/deflate/write.rs b/src/deflate/write.rs index 596450348..27c02b595 100644 --- a/src/deflate/write.rs +++ b/src/deflate/write.rs @@ -26,7 +26,7 @@ use {Compress, Decompress}; /// // Vec implements Write to print the compressed bytes of sample string /// # fn main() { /// -/// let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); /// e.write(b"Hello World").unwrap(); /// println!("{:?}", e.finish().unwrap()); /// # } @@ -200,7 +200,7 @@ impl AsyncRead for DeflateEncoder {} /// use flate2::write::DeflateDecoder; /// /// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_writer(bytes).unwrap()); diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs index 22be33ae3..eebbd36a5 100644 --- a/src/gz/bufread.rs +++ b/src/gz/bufread.rs @@ -133,7 +133,7 @@ fn read_gz_header(r: &mut R) -> io::Result
{ /// fn open_hello_world() -> io::Result> { /// let f = File::open("examples/hello_world.txt")?; /// let b = BufReader::new(f); -/// let mut gz = GzEncoder::new(b, Compression::Fast); +/// let mut gz = GzEncoder::new(b, Compression::fast()); /// let mut buffer = Vec::new(); /// gz.read_to_end(&mut buffer)?; /// Ok(buffer) @@ -262,7 +262,7 @@ impl Write for GzEncoder { /// use flate2::bufread::GzDecoder; /// /// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); @@ -409,7 +409,7 @@ impl Write for GzDecoder { /// use flate2::bufread::MultiGzDecoder; /// /// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 5fc7c7c1d..8f2c24b59 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -94,7 +94,7 @@ impl Header { /// let mut gz = GzBuilder::new() /// .filename("hello_world.txt") /// .comment("test file, please delete") -/// .write(f, Compression::Default); +/// .write(f, Compression::default()); /// gz.write(b"hello world")?; /// gz.finish()?; /// # Ok(()) @@ -218,11 +218,7 @@ impl Builder { header[5] = (mtime >> 8) as u8; header[6] = (mtime >> 16) as u8; header[7] = (mtime >> 24) as u8; - header[8] = match lvl { - Compression::Best => 2, - Compression::Fast => 4, - _ => 0, - }; + header[8] = lvl.0 as u8; // Typically this byte indicates what OS the gz stream was created on, // but in an effort to have cross-platform reproducible streams just @@ -238,12 +234,12 @@ mod tests { use std::io::prelude::*; use super::{read, write, Builder}; - use Compression::Default; + use Compression; use rand::{thread_rng, Rng}; #[test] fn roundtrip() { - let mut e = write::GzEncoder::new(Vec::new(), Default); + let mut e = write::GzEncoder::new(Vec::new(), Compression::default()); e.write_all(b"foo bar baz").unwrap(); let inner = e.finish().unwrap(); let mut d = read::GzDecoder::new(&inner[..]).unwrap(); @@ -254,7 +250,7 @@ mod tests { #[test] fn roundtrip_zero() { - let e = write::GzEncoder::new(Vec::new(), Default); + let e = write::GzEncoder::new(Vec::new(), Compression::default()); let inner = e.finish().unwrap(); let mut d = read::GzDecoder::new(&inner[..]).unwrap(); let mut s = String::new(); @@ -265,7 +261,7 @@ mod tests { #[test] fn roundtrip_big() { let mut real = Vec::new(); - let mut w = write::GzEncoder::new(Vec::new(), Default); + let mut w = write::GzEncoder::new(Vec::new(), Compression::default()); let v = thread_rng().gen_iter::().take(1024).collect::>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0, v.len())]; @@ -285,7 +281,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut r = read::GzDecoder::new(read::GzEncoder::new(&v[..], Default)).unwrap(); + let mut r = read::GzDecoder::new(read::GzEncoder::new(&v[..], Compression::default())).unwrap(); let mut res = Vec::new(); r.read_to_end(&mut res).unwrap(); assert!(res == v); @@ -298,7 +294,7 @@ mod tests { .filename("foo.rs") .comment("bar") .extra(vec![0, 1, 2, 3]) - .read(&r[..], Default); + .read(&r[..], Compression::default()); let mut d = read::GzDecoder::new(e).unwrap(); assert_eq!(d.header().filename(), Some(&b"foo.rs"[..])); assert_eq!(d.header().comment(), Some(&b"bar"[..])); @@ -310,7 +306,7 @@ mod tests { #[test] fn keep_reading_after_end() { - let mut e = write::GzEncoder::new(Vec::new(), Default); + let mut e = write::GzEncoder::new(Vec::new(), Compression::default()); e.write_all(b"foo bar baz").unwrap(); let inner = e.finish().unwrap(); let mut d = read::GzDecoder::new(&inner[..]).unwrap(); @@ -326,7 +322,7 @@ mod tests { ::quickcheck::quickcheck(test as fn(_) -> _); fn test(v: Vec) -> bool { - let r = read::GzEncoder::new(&v[..], Default); + let r = read::GzEncoder::new(&v[..], Compression::default()); let mut r = read::GzDecoder::new(r).unwrap(); let mut v2 = Vec::new(); r.read_to_end(&mut v2).unwrap(); @@ -336,7 +332,7 @@ mod tests { #[test] fn flush_after_write() { - let mut f = write::GzEncoder::new(Vec::new(), Default); + let mut f = write::GzEncoder::new(Vec::new(), Compression::default()); write!(f, "Hello world").unwrap(); f.flush().unwrap(); } diff --git a/src/gz/read.rs b/src/gz/read.rs index c95e388a5..84780cbba 100644 --- a/src/gz/read.rs +++ b/src/gz/read.rs @@ -27,7 +27,7 @@ use super::bufread; /// fn gzencode_hello_world() -> io::Result> { /// let mut ret_vec = [0;100]; /// let bytestring = b"hello world"; -/// let mut gz = GzEncoder::new(&bytestring[..], Compression::Fast); +/// let mut gz = GzEncoder::new(&bytestring[..], Compression::fast()); /// let count = gz.read(&mut ret_vec)?; /// Ok(ret_vec[0..count].to_vec()) /// } @@ -110,7 +110,7 @@ impl Write for GzEncoder { /// use flate2::read::GzDecoder; /// /// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); @@ -209,7 +209,7 @@ impl Write for GzDecoder { /// use flate2::read::MultiGzDecoder; /// /// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/src/gz/write.rs b/src/gz/write.rs index a9a10569a..837d4a992 100644 --- a/src/gz/write.rs +++ b/src/gz/write.rs @@ -28,7 +28,7 @@ use zio; /// // Vec implements Write to print the compressed bytes of sample string /// # fn main() { /// -/// let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// e.write(b"Hello World").unwrap(); /// println!("{:?}", e.finish().unwrap()); /// # } diff --git a/src/lib.rs b/src/lib.rs index a6f987f24..d02c1f8ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ //! //! # fn main() { let _ = run(); } //! # fn run() -> io::Result<()> { -//! let mut encoder = GzEncoder::new(Vec::new(), Compression::Default); +//! let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); //! encoder.write(b"Example")?; //! # Ok(()) //! # } @@ -80,9 +80,6 @@ extern crate rand; #[macro_use] extern crate tokio_io; -use std::io::prelude::*; -use std::io; - pub use gz::Builder as GzBuilder; pub use gz::Header as GzHeader; pub use mem::{Compress, DataError, Decompress, Flush, Status}; @@ -157,21 +154,43 @@ fn _assert_send_sync() { /// When compressing data, the compression level can be specified by a value in /// this enum. #[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub enum Compression { +pub struct Compression(u32); + +impl Compression { + /// Creates a new description of the compression level with an explicitly + /// specified integer. + /// + /// The integer here is typically on a scale of 0-9 where 0 means "no + /// compression" and 9 means "take as long as you'd like". + pub fn new(level: u32) -> Compression { + Compression(level) + } + /// No compression is to be performed, this may actually inflate data /// slightly when encoding. - None = 0, + pub fn none() -> Compression { + Compression(0) + } + /// Optimize for the best speed of encoding. - Fast = 1, + pub fn fast() -> Compression { + Compression(1) + } + /// Optimize for the size of data being encoded. - Best = 9, - /// Choose the default compression, a balance between speed and size. - Default = 6, + pub fn best() -> Compression { + Compression(9) + } + + /// Returns an integer representing the compression level, typically on a + /// scale of 0-9 + pub fn level(&self) -> u32 { + self.0 + } } -/// Default to Compression::Default. impl Default for Compression { fn default() -> Compression { - Compression::Default + Compression(6) } } diff --git a/src/mem.rs b/src/mem.rs index 2212cb2e6..da74f4f07 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -161,7 +161,7 @@ impl Compress { unsafe { let mut state = ffi::StreamWrapper::default(); let ret = ffi::mz_deflateInit2(&mut *state, - level as c_int, + level.0 as c_int, ffi::MZ_DEFLATED, if zlib_header { ffi::MZ_DEFAULT_WINDOW_BITS @@ -498,7 +498,7 @@ mod tests { let mut zlib = Vec::new(); let mut deflate = Vec::new(); - let comp = Compression::Default; + let comp = Compression::default(); write::ZlibEncoder::new(&mut zlib, comp).write_all(string).unwrap(); write::DeflateEncoder::new(&mut deflate, comp).write_all(string).unwrap(); diff --git a/src/zlib/bufread.rs b/src/zlib/bufread.rs index a21a905f6..058e54831 100644 --- a/src/zlib/bufread.rs +++ b/src/zlib/bufread.rs @@ -31,7 +31,7 @@ use {Compress, Decompress}; /// # fn open_hello_world() -> std::io::Result> { /// let f = File::open("examples/hello_world.txt")?; /// let b = BufReader::new(f); -/// let mut z = ZlibEncoder::new(b, Compression::Fast); +/// let mut z = ZlibEncoder::new(b, Compression::fast()); /// let mut buffer = Vec::new(); /// z.read_to_end(&mut buffer)?; /// # Ok(buffer) @@ -150,7 +150,7 @@ impl AsyncWrite for ZlibEncoder { /// use flate2::bufread::ZlibDecoder; /// /// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_bufreader(bytes).unwrap()); diff --git a/src/zlib/mod.rs b/src/zlib/mod.rs index b04e40b82..a407cdfe6 100644 --- a/src/zlib/mod.rs +++ b/src/zlib/mod.rs @@ -11,12 +11,12 @@ mod tests { use rand::{thread_rng, Rng}; use zlib::{read, write}; - use Compression::Default; + use Compression; #[test] fn roundtrip() { let mut real = Vec::new(); - let mut w = write::ZlibEncoder::new(Vec::new(), Default); + let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); let v = thread_rng().gen_iter::().take(1024).collect::>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0, v.len())]; @@ -33,7 +33,7 @@ mod tests { #[test] fn drop_writes() { let mut data = Vec::new(); - write::ZlibEncoder::new(&mut data, Default) + write::ZlibEncoder::new(&mut data, Compression::default()) .write_all(b"foo") .unwrap(); let mut r = read::ZlibDecoder::new(&data[..]); @@ -45,7 +45,7 @@ mod tests { #[test] fn total_in() { let mut real = Vec::new(); - let mut w = write::ZlibEncoder::new(Vec::new(), Default); + let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); let v = thread_rng().gen_iter::().take(1024).collect::>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0, v.len())]; @@ -73,7 +73,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Default)); + let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Compression::default())); let mut ret = Vec::new(); r.read_to_end(&mut ret).unwrap(); assert_eq!(ret, v); @@ -85,7 +85,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Default); + let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Compression::default()); w.write_all(&v).unwrap(); let w = w.finish().unwrap().finish().unwrap(); assert!(w == v); @@ -97,7 +97,7 @@ mod tests { .gen_iter::() .take(1024 * 1024) .collect::>(); - let mut w = write::ZlibEncoder::new(Vec::new(), Default); + let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); w.write_all(&v).unwrap(); let data = w.finish().unwrap(); @@ -144,7 +144,7 @@ mod tests { ::quickcheck::quickcheck(test as fn(_) -> _); fn test(v: Vec) -> bool { - let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Default)); + let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Compression::default())); let mut v2 = Vec::new(); r.read_to_end(&mut v2).unwrap(); v == v2 @@ -156,7 +156,7 @@ mod tests { ::quickcheck::quickcheck(test as fn(_) -> _); fn test(v: Vec) -> bool { - let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Default); + let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Compression::default()); w.write_all(&v).unwrap(); v == w.finish().unwrap().finish().unwrap() } diff --git a/src/zlib/read.rs b/src/zlib/read.rs index a2eece78d..78087687d 100644 --- a/src/zlib/read.rs +++ b/src/zlib/read.rs @@ -28,7 +28,7 @@ use super::bufread; /// /// # fn open_hello_world() -> std::io::Result> { /// let f = File::open("examples/hello_world.txt")?; -/// let mut z = ZlibEncoder::new(f, Compression::Fast); +/// let mut z = ZlibEncoder::new(f, Compression::fast()); /// let mut buffer = [0;50]; /// let byte_count = z.read(&mut buffer)?; /// # Ok(buffer[0..byte_count].to_vec()) @@ -147,7 +147,7 @@ impl AsyncWrite for ZlibEncoder { /// use flate2::read::ZlibDecoder; /// /// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/src/zlib/write.rs b/src/zlib/write.rs index 52e4fc28e..e7a4d85c9 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -26,7 +26,7 @@ use {Compress, Decompress}; /// // Vec implements Write, assigning the compressed bytes of sample string /// /// # fn zlib_encoding() -> std::io::Result<()> { -/// let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); /// e.write(b"Hello World")?; /// let compressed = e.finish()?; /// # Ok(()) @@ -202,7 +202,7 @@ impl AsyncRead for ZlibEncoder {} /// use flate2::write::ZlibDecoder; /// /// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::default()); /// # e.write(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); diff --git a/tests/early-flush.rs b/tests/early-flush.rs index 70c10673f..e13a044bd 100644 --- a/tests/early-flush.rs +++ b/tests/early-flush.rs @@ -7,7 +7,7 @@ use flate2::read::GzDecoder; #[test] fn smoke() { - let mut w = GzEncoder::new(Vec::new(), flate2::Compression::Default); + let mut w = GzEncoder::new(Vec::new(), flate2::Compression::default()); w.flush().unwrap(); w.write(b"hello").unwrap(); diff --git a/tests/zero-write.rs b/tests/zero-write.rs index 8174e0c39..f0db86cb8 100644 --- a/tests/zero-write.rs +++ b/tests/zero-write.rs @@ -3,6 +3,6 @@ extern crate flate2; #[test] fn zero_write_is_error() { let mut buf = [0u8]; - let writer = flate2::write::DeflateEncoder::new(&mut buf[..], flate2::Compression::Default); + let writer = flate2::write::DeflateEncoder::new(&mut buf[..], flate2::Compression::default()); assert!(writer.finish().is_err()); }