11use std:: io;
22use std:: io:: prelude:: * ;
3+ use std:: marker:: PhantomData ;
34use std:: mem;
45
5- use crate :: { Compress , Decompress , DecompressError , FlushCompress , FlushDecompress , Status } ;
6+ use crate :: { Compress , CompressError , Decompress , DecompressError , FlushCompress , FlushDecompress , Status } ;
67
78#[ derive( Debug ) ]
8- pub struct Writer < W : Write , D : Ops > {
9+ pub struct Writer < W : Write , DE : Into < io :: Error > , D : Ops < DE > > where io :: Error : From < DE > {
910 obj : Option < W > ,
1011 pub data : D ,
1112 buf : Vec < u8 > ,
13+ _writer_error : PhantomData < DE > ,
1214}
1315
14- pub trait Ops {
16+ pub trait Ops < Error > {
1517 type Flush : Flush ;
1618 fn total_in ( & self ) -> u64 ;
1719 fn total_out ( & self ) -> u64 ;
@@ -20,16 +22,16 @@ pub trait Ops {
2022 input : & [ u8 ] ,
2123 output : & mut [ u8 ] ,
2224 flush : Self :: Flush ,
23- ) -> Result < Status , DecompressError > ;
25+ ) -> Result < Status , Error > ;
2426 fn run_vec (
2527 & mut self ,
2628 input : & [ u8 ] ,
2729 output : & mut Vec < u8 > ,
2830 flush : Self :: Flush ,
29- ) -> Result < Status , DecompressError > ;
31+ ) -> Result < Status , Error > ;
3032}
3133
32- impl Ops for Compress {
34+ impl Ops < CompressError > for Compress {
3335 type Flush = FlushCompress ;
3436 fn total_in ( & self ) -> u64 {
3537 self . total_in ( )
@@ -42,20 +44,20 @@ impl Ops for Compress {
4244 input : & [ u8 ] ,
4345 output : & mut [ u8 ] ,
4446 flush : FlushCompress ,
45- ) -> Result < Status , DecompressError > {
46- Ok ( self . compress ( input, output, flush) . unwrap ( ) )
47+ ) -> Result < Status , CompressError > {
48+ self . compress ( input, output, flush)
4749 }
4850 fn run_vec (
4951 & mut self ,
5052 input : & [ u8 ] ,
5153 output : & mut Vec < u8 > ,
5254 flush : FlushCompress ,
53- ) -> Result < Status , DecompressError > {
54- Ok ( self . compress_vec ( input, output, flush) . unwrap ( ) )
55+ ) -> Result < Status , CompressError > {
56+ self . compress_vec ( input, output, flush)
5557 }
5658}
5759
58- impl Ops for Decompress {
60+ impl Ops < DecompressError > for Decompress {
5961 type Flush = FlushDecompress ;
6062 fn total_in ( & self ) -> u64 {
6163 self . total_in ( )
@@ -115,10 +117,10 @@ impl Flush for FlushDecompress {
115117 }
116118}
117119
118- pub fn read < R , D > ( obj : & mut R , data : & mut D , dst : & mut [ u8 ] ) -> io:: Result < usize >
120+ pub fn read < R , DE , D > ( obj : & mut R , data : & mut D , dst : & mut [ u8 ] ) -> io:: Result < usize >
119121where
120122 R : BufRead ,
121- D : Ops ,
123+ D : Ops < DE > ,
122124{
123125 loop {
124126 let ( read, consumed, ret, eof) ;
@@ -156,12 +158,13 @@ where
156158 }
157159}
158160
159- impl < W : Write , D : Ops > Writer < W , D > {
160- pub fn new ( w : W , d : D ) -> Writer < W , D > {
161+ impl < W : Write , DE : Into < io :: Error > , D : Ops < DE > > Writer < W , DE , D > where io :: Error : From < DE > {
162+ pub fn new ( w : W , d : D ) -> Writer < W , DE , D > {
161163 Writer {
162164 obj : Some ( w) ,
163165 data : d,
164166 buf : Vec :: with_capacity ( 32 * 1024 ) ,
167+ _writer_error : PhantomData ,
165168 }
166169 }
167170
@@ -247,15 +250,14 @@ impl<W: Write, D: Ops> Writer<W, D> {
247250 }
248251}
249252
250- impl < W : Write , D : Ops > Write for Writer < W , D > {
253+ impl < W : Write , DE : Into < io :: Error > , D : Ops < DE > > Write for Writer < W , DE , D > where io :: Error : From < DE > {
251254 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
252255 self . write_with_status ( buf) . map ( |res| res. 0 )
253256 }
254257
255258 fn flush ( & mut self ) -> io:: Result < ( ) > {
256259 self . data
257- . run_vec ( & [ ] , & mut self . buf , D :: Flush :: sync ( ) )
258- . unwrap ( ) ;
260+ . run_vec ( & [ ] , & mut self . buf , D :: Flush :: sync ( ) ) ?;
259261
260262 // Unfortunately miniz doesn't actually tell us when we're done with
261263 // pulling out all the data from the internal stream. To remedy this we
@@ -266,18 +268,17 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {
266268 self . dump ( ) ?;
267269 let before = self . data . total_out ( ) ;
268270 self . data
269- . run_vec ( & [ ] , & mut self . buf , D :: Flush :: none ( ) )
270- . unwrap ( ) ;
271+ . run_vec ( & [ ] , & mut self . buf , D :: Flush :: none ( ) ) ?;
271272 if before == self . data . total_out ( ) {
272273 break ;
273274 }
274275 }
275276
276- self . obj . as_mut ( ) . unwrap ( ) . flush ( )
277+ self . obj . as_mut ( ) . map ( Write :: flush ) . unwrap_or ( Ok ( ( ) ) )
277278 }
278279}
279280
280- impl < W : Write , D : Ops > Drop for Writer < W , D > {
281+ impl < W : Write , DE : Into < io :: Error > , D : Ops < DE > > Drop for Writer < W , DE , D > where io :: Error : From < DE > {
281282 fn drop ( & mut self ) {
282283 if self . obj . is_some ( ) {
283284 let _ = self . finish ( ) ;
0 commit comments