11use std:: io;
22use std:: io:: prelude:: * ;
3- use std:: marker:: PhantomData ;
43use std:: mem;
54
65use crate :: { Compress , CompressError , Decompress , DecompressError , FlushCompress , FlushDecompress , Status } ;
76
87#[ derive( Debug ) ]
9- pub struct Writer < W : Write , DE : Into < io :: Error > , D : Ops < DE > > where io :: Error : From < DE > {
8+ pub struct Writer < W : Write , D : Ops > {
109 obj : Option < W > ,
1110 pub data : D ,
1211 buf : Vec < u8 > ,
13- _writer_error : PhantomData < DE > ,
1412}
1513
16- pub trait Ops < Error > {
14+ pub trait Ops {
15+ type Error : Into < io:: Error > ;
1716 type Flush : Flush ;
1817 fn total_in ( & self ) -> u64 ;
1918 fn total_out ( & self ) -> u64 ;
@@ -22,16 +21,17 @@ pub trait Ops<Error> {
2221 input : & [ u8 ] ,
2322 output : & mut [ u8 ] ,
2423 flush : Self :: Flush ,
25- ) -> Result < Status , Error > ;
24+ ) -> Result < Status , Self :: Error > ;
2625 fn run_vec (
2726 & mut self ,
2827 input : & [ u8 ] ,
2928 output : & mut Vec < u8 > ,
3029 flush : Self :: Flush ,
31- ) -> Result < Status , Error > ;
30+ ) -> Result < Status , Self :: Error > ;
3231}
3332
34- impl Ops < CompressError > for Compress {
33+ impl Ops for Compress {
34+ type Error = CompressError ;
3535 type Flush = FlushCompress ;
3636 fn total_in ( & self ) -> u64 {
3737 self . total_in ( )
@@ -57,7 +57,8 @@ impl Ops<CompressError> for Compress {
5757 }
5858}
5959
60- impl Ops < DecompressError > for Decompress {
60+ impl Ops for Decompress {
61+ type Error = DecompressError ;
6162 type Flush = FlushDecompress ;
6263 fn total_in ( & self ) -> u64 {
6364 self . total_in ( )
@@ -117,10 +118,10 @@ impl Flush for FlushDecompress {
117118 }
118119}
119120
120- pub fn read < R , DE , D > ( obj : & mut R , data : & mut D , dst : & mut [ u8 ] ) -> io:: Result < usize >
121+ pub fn read < R , D > ( obj : & mut R , data : & mut D , dst : & mut [ u8 ] ) -> io:: Result < usize >
121122where
122123 R : BufRead ,
123- D : Ops < DE > ,
124+ D : Ops ,
124125{
125126 loop {
126127 let ( read, consumed, ret, eof) ;
@@ -158,13 +159,12 @@ where
158159 }
159160}
160161
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 > {
162+ impl < W : Write , D : Ops > Writer < W , D > {
163+ pub fn new ( w : W , d : D ) -> Writer < W , D > {
163164 Writer {
164165 obj : Some ( w) ,
165166 data : d,
166167 buf : Vec :: with_capacity ( 32 * 1024 ) ,
167- _writer_error : PhantomData ,
168168 }
169169 }
170170
@@ -173,7 +173,7 @@ impl<W: Write, DE: Into<io::Error>, D: Ops<DE>> Writer<W, DE, D> where io::Error
173173 self . dump ( ) ?;
174174
175175 let before = self . data . total_out ( ) ;
176- self . data . run_vec ( & [ ] , & mut self . buf , D :: Flush :: finish ( ) ) ?;
176+ self . data . run_vec ( & [ ] , & mut self . buf , Flush :: finish ( ) ) . map_err ( Into :: into ) ?;
177177 if before == self . data . total_out ( ) {
178178 return Ok ( ( ) ) ;
179179 }
@@ -250,14 +250,15 @@ impl<W: Write, DE: Into<io::Error>, D: Ops<DE>> Writer<W, DE, D> where io::Error
250250 }
251251}
252252
253- impl < W : Write , DE : Into < io :: Error > , D : Ops < DE > > Write for Writer < W , DE , D > where io :: Error : From < DE > {
253+ impl < W : Write , D : Ops > Write for Writer < W , D > {
254254 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
255255 self . write_with_status ( buf) . map ( |res| res. 0 )
256256 }
257257
258- fn flush ( & mut self ) -> io:: Result < ( ) > {
258+ fn flush ( & mut self ) -> io:: Result < ( ) >
259+ {
259260 self . data
260- . run_vec ( & [ ] , & mut self . buf , D :: Flush :: sync ( ) ) ?;
261+ . run_vec ( & [ ] , & mut self . buf , Flush :: sync ( ) ) . map_err ( Into :: into ) ?;
261262
262263 // Unfortunately miniz doesn't actually tell us when we're done with
263264 // pulling out all the data from the internal stream. To remedy this we
@@ -268,7 +269,7 @@ impl<W: Write, DE: Into<io::Error>, D: Ops<DE>> Write for Writer<W, DE, D> where
268269 self . dump ( ) ?;
269270 let before = self . data . total_out ( ) ;
270271 self . data
271- . run_vec ( & [ ] , & mut self . buf , D :: Flush :: none ( ) ) ?;
272+ . run_vec ( & [ ] , & mut self . buf , Flush :: none ( ) ) . map_err ( Into :: into ) ?;
272273 if before == self . data . total_out ( ) {
273274 break ;
274275 }
@@ -278,7 +279,7 @@ impl<W: Write, DE: Into<io::Error>, D: Ops<DE>> Write for Writer<W, DE, D> where
278279 }
279280}
280281
281- impl < W : Write , DE : Into < io :: Error > , D : Ops < DE > > Drop for Writer < W , DE , D > where io :: Error : From < DE > {
282+ impl < W : Write , D : Ops > Drop for Writer < W , D > {
282283 fn drop ( & mut self ) {
283284 if self . obj . is_some ( ) {
284285 let _ = self . finish ( ) ;
0 commit comments