diff options
Diffstat (limited to 'src/zlib')
-rw-r--r-- | src/zlib/bufread.rs | 30 | ||||
-rw-r--r-- | src/zlib/mod.rs | 14 | ||||
-rw-r--r-- | src/zlib/read.rs | 53 | ||||
-rw-r--r-- | src/zlib/write.rs | 19 |
4 files changed, 95 insertions, 21 deletions
diff --git a/src/zlib/bufread.rs b/src/zlib/bufread.rs index f1d3231..85bbd38 100644 --- a/src/zlib/bufread.rs +++ b/src/zlib/bufread.rs @@ -7,9 +7,10 @@ use crate::{Compress, Decompress}; /// A ZLIB encoder, or compressor. /// -/// This structure consumes a [`BufRead`] interface, reading uncompressed data -/// from the underlying reader, and emitting compressed data. +/// This structure implements a [`Read`] interface. When read from, it reads +/// uncompressed data from the underlying [`BufRead`] and provides the compressed data. /// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html /// /// # Examples @@ -47,6 +48,15 @@ impl<R: BufRead> ZlibEncoder<R> { data: Compress::new(level, true), } } + + /// Creates a new encoder with the given `compression` settings which will + /// read uncompressed data from the given stream `r` and emit the compressed stream. + pub fn new_with_compress(r: R, compression: Compress) -> ZlibEncoder<R> { + ZlibEncoder { + obj: r, + data: compression, + } + } } pub fn reset_encoder_data<R>(zlib: &mut ZlibEncoder<R>) { @@ -119,9 +129,10 @@ impl<R: BufRead + Write> Write for ZlibEncoder<R> { /// A ZLIB decoder, or decompressor. /// -/// This structure consumes a [`BufRead`] interface, reading compressed data -/// from the underlying reader, and emitting uncompressed data. +/// This structure implements a [`Read`] interface. When read from, it reads +/// compressed data from the underlying [`BufRead`] and provides the uncompressed data. /// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html /// /// # Examples @@ -165,6 +176,15 @@ impl<R: BufRead> ZlibDecoder<R> { data: Decompress::new(true), } } + + /// Creates a new decoder which will decompress data read from the given + /// stream, using the given `decompression` settings. + pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> { + ZlibDecoder { + obj: r, + data: decompression, + } + } } pub fn reset_decoder_data<R>(zlib: &mut ZlibDecoder<R>) { @@ -192,7 +212,7 @@ impl<R> ZlibDecoder<R> { /// Acquires a mutable reference to the underlying stream /// /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. + /// this decoder is continued to be used. pub fn get_mut(&mut self) -> &mut R { &mut self.obj } diff --git a/src/zlib/mod.rs b/src/zlib/mod.rs index 9d3de95..1a293ba 100644 --- a/src/zlib/mod.rs +++ b/src/zlib/mod.rs @@ -19,14 +19,14 @@ mod tests { let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0..v.len())]; - real.extend(to_write.iter().map(|x| *x)); + real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } let result = w.finish().unwrap(); let mut r = read::ZlibDecoder::new(&result[..]); let mut ret = Vec::new(); r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); + assert_eq!(ret, real); } #[test] @@ -38,7 +38,7 @@ mod tests { let mut r = read::ZlibDecoder::new(&data[..]); let mut ret = Vec::new(); r.read_to_end(&mut ret).unwrap(); - assert!(ret == b"foo"); + assert_eq!(ret, b"foo"); } #[test] @@ -48,7 +48,7 @@ mod tests { let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); for _ in 0..200 { let to_write = &v[..thread_rng().gen_range(0..v.len())]; - real.extend(to_write.iter().map(|x| *x)); + real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } let mut result = w.finish().unwrap(); @@ -56,13 +56,13 @@ mod tests { let result_len = result.len(); for _ in 0..200 { - result.extend(v.iter().map(|x| *x)); + result.extend(v.iter().copied()); } let mut r = read::ZlibDecoder::new(&result[..]); let mut ret = Vec::new(); r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); + assert_eq!(ret, real); assert_eq!(r.total_in(), result_len as u64); } @@ -82,7 +82,7 @@ mod tests { 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); + assert_eq!(w, v); } #[test] diff --git a/src/zlib/read.rs b/src/zlib/read.rs index 5094931..3b41ae6 100644 --- a/src/zlib/read.rs +++ b/src/zlib/read.rs @@ -3,11 +3,12 @@ use std::io::prelude::*; use super::bufread; use crate::bufreader::BufReader; +use crate::Decompress; /// A ZLIB encoder, or compressor. /// -/// This structure implements a [`Read`] interface and will read uncompressed -/// data from an underlying stream and emit a stream of compressed data. +/// This structure implements a [`Read`] interface. When read from, it reads +/// uncompressed data from the underlying [`Read`] and provides the compressed data. /// /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html /// @@ -24,9 +25,9 @@ use crate::bufreader::BufReader; /// # 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 buffer = [0;50]; -/// let byte_count = z.read(&mut buffer)?; -/// # Ok(buffer[0..byte_count].to_vec()) +/// let mut buffer = Vec::new(); +/// z.read_to_end(&mut buffer)?; +/// # Ok(buffer) /// # } /// ``` #[derive(Debug)] @@ -42,6 +43,14 @@ impl<R: Read> ZlibEncoder<R> { inner: bufread::ZlibEncoder::new(BufReader::new(r), level), } } + + /// Creates a new encoder with the given `compression` settings which will + /// read uncompressed data from the given stream `r` and emit the compressed stream. + pub fn new_with_compress(r: R, compression: crate::Compress) -> ZlibEncoder<R> { + ZlibEncoder { + inner: bufread::ZlibEncoder::new_with_compress(BufReader::new(r), compression), + } + } } impl<R> ZlibEncoder<R> { @@ -117,8 +126,8 @@ impl<W: Read + Write> Write for ZlibEncoder<W> { /// A ZLIB decoder, or decompressor. /// -/// This structure implements a [`Read`] interface and takes a stream of -/// compressed data as input, providing the decompressed data when read from. +/// This structure implements a [`Read`] interface. When read from, it reads +/// compressed data from the underlying [`Read`] and provides the uncompressed data. /// /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html /// @@ -160,7 +169,8 @@ impl<R: Read> ZlibDecoder<R> { ZlibDecoder::new_with_buf(r, vec![0; 32 * 1024]) } - /// Same as `new`, but the intermediate buffer for data is specified. + /// Creates a new decoder which will decompress data read from the given + /// stream `r`, using `buf` as backing to speed up reading. /// /// Note that the specified buffer will only be used up to its current /// length. The buffer's capacity will also not grow over time. @@ -169,6 +179,31 @@ impl<R: Read> ZlibDecoder<R> { inner: bufread::ZlibDecoder::new(BufReader::with_buf(buf, r)), } } + + /// Creates a new decoder which will decompress data read from the given + /// stream `r`, along with `decompression` settings. + pub fn new_with_decompress(r: R, decompression: Decompress) -> ZlibDecoder<R> { + ZlibDecoder::new_with_decompress_and_buf(r, vec![0; 32 * 1024], decompression) + } + + /// Creates a new decoder which will decompress data read from the given + /// stream `r`, using `buf` as backing to speed up reading, + /// along with `decompression` settings to configure decoder. + /// + /// Note that the specified buffer will only be used up to its current + /// length. The buffer's capacity will also not grow over time. + pub fn new_with_decompress_and_buf( + r: R, + buf: Vec<u8>, + decompression: Decompress, + ) -> ZlibDecoder<R> { + ZlibDecoder { + inner: bufread::ZlibDecoder::new_with_decompress( + BufReader::with_buf(buf, r), + decompression, + ), + } + } } impl<R> ZlibDecoder<R> { @@ -195,7 +230,7 @@ impl<R> ZlibDecoder<R> { /// Acquires a mutable reference to the underlying stream /// /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. + /// this decoder is continued to be used. pub fn get_mut(&mut self) -> &mut R { self.inner.get_mut().get_mut() } diff --git a/src/zlib/write.rs b/src/zlib/write.rs index c671814..d8ad2f2 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -44,6 +44,14 @@ impl<W: Write> ZlibEncoder<W> { } } + /// Creates a new encoder which will write compressed data to the stream + /// `w` with the given `compression` settings. + pub fn new_with_compress(w: W, compression: Compress) -> ZlibEncoder<W> { + ZlibEncoder { + inner: zio::Writer::new(w, compression), + } + } + /// Acquires a reference to the underlying writer. pub fn get_ref(&self) -> &W { self.inner.get_ref() @@ -218,6 +226,17 @@ impl<W: Write> ZlibDecoder<W> { } } + /// Creates a new decoder which will write uncompressed data to the stream `w` + /// using the given `decompression` settings. + /// + /// When this decoder is dropped or unwrapped the final pieces of data will + /// be flushed. + pub fn new_with_decompress(w: W, decompression: Decompress) -> ZlibDecoder<W> { + ZlibDecoder { + inner: zio::Writer::new(w, decompression), + } + } + /// Acquires a reference to the underlying writer. pub fn get_ref(&self) -> &W { self.inner.get_ref() |