diff --git a/src/zio.rs b/src/zio.rs index ea25922a..3e6b9eb3 100644 --- a/src/zio.rs +++ b/src/zio.rs @@ -2,7 +2,9 @@ use std::io; use std::io::prelude::*; use std::mem; -use crate::{Compress, Decompress, DecompressError, FlushCompress, FlushDecompress, Status}; +use crate::{ + Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status, +}; #[derive(Debug)] pub struct Writer { @@ -12,6 +14,7 @@ pub struct Writer { } pub trait Ops { + type Error: Into; type Flush: Flush; fn total_in(&self) -> u64; fn total_out(&self) -> u64; @@ -20,16 +23,17 @@ pub trait Ops { input: &[u8], output: &mut [u8], flush: Self::Flush, - ) -> Result; + ) -> Result; fn run_vec( &mut self, input: &[u8], output: &mut Vec, flush: Self::Flush, - ) -> Result; + ) -> Result; } impl Ops for Compress { + type Error = CompressError; type Flush = FlushCompress; fn total_in(&self) -> u64 { self.total_in() @@ -42,20 +46,21 @@ impl Ops for Compress { input: &[u8], output: &mut [u8], flush: FlushCompress, - ) -> Result { - Ok(self.compress(input, output, flush).unwrap()) + ) -> Result { + self.compress(input, output, flush) } fn run_vec( &mut self, input: &[u8], output: &mut Vec, flush: FlushCompress, - ) -> Result { - Ok(self.compress_vec(input, output, flush).unwrap()) + ) -> Result { + self.compress_vec(input, output, flush) } } impl Ops for Decompress { + type Error = DecompressError; type Flush = FlushDecompress; fn total_in(&self) -> u64 { self.total_in() @@ -170,7 +175,9 @@ impl Writer { self.dump()?; let before = self.data.total_out(); - self.data.run_vec(&[], &mut self.buf, D::Flush::finish())?; + self.data + .run_vec(&[], &mut self.buf, Flush::finish()) + .map_err(Into::into)?; if before == self.data.total_out() { return Ok(()); } @@ -254,8 +261,8 @@ impl Write for Writer { fn flush(&mut self) -> io::Result<()> { self.data - .run_vec(&[], &mut self.buf, D::Flush::sync()) - .unwrap(); + .run_vec(&[], &mut self.buf, Flush::sync()) + .map_err(Into::into)?; // Unfortunately miniz doesn't actually tell us when we're done with // pulling out all the data from the internal stream. To remedy this we @@ -266,8 +273,8 @@ impl Write for Writer { self.dump()?; let before = self.data.total_out(); self.data - .run_vec(&[], &mut self.buf, D::Flush::none()) - .unwrap(); + .run_vec(&[], &mut self.buf, Flush::none()) + .map_err(Into::into)?; if before == self.data.total_out() { break; }