Skip to content

Commit

Permalink
Switch Compression to a struct
Browse files Browse the repository at this point in the history
No need for it to be an enum for matching and otherwise this allows usage
of custom compression levels.

Closes rust-lang#112
  • Loading branch information
alexcrichton committed Nov 7, 2017
1 parent 367f295 commit 7e0390b
Show file tree
Hide file tree
Showing 36 changed files with 108 additions and 93 deletions.
2 changes: 1 addition & 1 deletion examples/deflatedecoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/deflatedecoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/deflatedecoder-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/deflateencoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
fn open_hello_world() -> io::Result<Vec<u8>> {
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)
Expand Down
2 changes: 1 addition & 1 deletion examples/deflateencoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> {
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())
}
2 changes: 1 addition & 1 deletion examples/deflateencoder-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use flate2::write::DeflateEncoder;

// Vec<u8> 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());
}
2 changes: 1 addition & 1 deletion examples/gzbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion examples/gzdecoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/gzdecoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/gzencoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
fn open_hello_world() -> io::Result<Vec<u8>> {
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)
Expand Down
2 changes: 1 addition & 1 deletion examples/gzencoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
fn gzencoder_read_hello_world() -> io::Result<Vec<u8>> {
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())
}
2 changes: 1 addition & 1 deletion examples/gzencoder-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use flate2::write::GzEncoder;

// Vec<u8> 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());
}
2 changes: 1 addition & 1 deletion examples/gzmultidecoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/gzmultidecoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibdecoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibdecoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibdecoder-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibencoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
fn open_hello_world() -> io::Result<Vec<u8>> {
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)
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibencoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
// File implements Read
fn open_hello_world() -> std::io::Result<Vec<u8>> {
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())
Expand Down
2 changes: 1 addition & 1 deletion examples/zlibencoder-write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use flate2::write::ZlibEncoder;

// Vec<u8> 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());
}
4 changes: 2 additions & 2 deletions src/deflate/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use {Compress, Decompress};
/// fn open_hello_world() -> io::Result<Vec<u8>> {
/// 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)
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<R: AsyncWrite + BufRead> AsyncWrite for DeflateEncoder<R> {
/// 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());
Expand Down
28 changes: 14 additions & 14 deletions src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
Expand All @@ -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[..]);
Expand All @@ -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::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
Expand Down Expand Up @@ -71,7 +71,7 @@ mod tests {
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
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);
Expand All @@ -83,7 +83,7 @@ mod tests {
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
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);
Expand All @@ -95,13 +95,13 @@ mod tests {
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
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);
Expand All @@ -114,12 +114,12 @@ mod tests {
.take(1024 * 1024)
.collect::<Vec<_>>();
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);
}
Expand All @@ -130,7 +130,7 @@ mod tests {
.gen_iter::<u8>()
.take(1024 * 1024)
.collect::<Vec<_>>();
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();

Expand Down Expand Up @@ -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();
Expand All @@ -178,7 +178,7 @@ mod tests {
::quickcheck::quickcheck(test as fn(_) -> _);

fn test(v: Vec<u8>) -> 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
Expand All @@ -190,7 +190,7 @@ mod tests {
::quickcheck::quickcheck(test as fn(_) -> _);

fn test(v: Vec<u8>) -> 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()
}
Expand Down
4 changes: 2 additions & 2 deletions src/deflate/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::bufread;
/// fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> {
/// 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())
/// }
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<R: AsyncRead + AsyncWrite> AsyncWrite for DeflateEncoder<R> {
/// 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());
Expand Down
4 changes: 2 additions & 2 deletions src/deflate/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {Compress, Decompress};
/// // Vec<u8> 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());
/// # }
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<W: AsyncRead + AsyncWrite> AsyncRead for DeflateEncoder<W> {}
/// 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());
Expand Down
6 changes: 3 additions & 3 deletions src/gz/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<Header> {
/// fn open_hello_world() -> io::Result<Vec<u8>> {
/// 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)
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<R: BufRead + Write> Write for GzEncoder<R> {
/// 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());
Expand Down Expand Up @@ -409,7 +409,7 @@ impl<R: BufRead + Write> Write for GzDecoder<R> {
/// 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());
Expand Down
Loading

0 comments on commit 7e0390b

Please sign in to comment.