Skip to content

Commit f8c910f

Browse files
committed
move to associated types
1 parent dcac32f commit f8c910f

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

src/deflate/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io;
22
use std::io::prelude::*;
33

4-
use crate::{zio, CompressError, DecompressError};
4+
use crate::zio;
55
use crate::{Compress, Decompress};
66

77
/// A DEFLATE encoder, or compressor.
@@ -28,7 +28,7 @@ use crate::{Compress, Decompress};
2828
/// ```
2929
#[derive(Debug)]
3030
pub struct DeflateEncoder<W: Write> {
31-
inner: zio::Writer<W, CompressError, Compress>,
31+
inner: zio::Writer<W, Compress>,
3232
}
3333

3434
impl<W: Write> DeflateEncoder<W> {
@@ -206,7 +206,7 @@ impl<W: Read + Write> Read for DeflateEncoder<W> {
206206
/// ```
207207
#[derive(Debug)]
208208
pub struct DeflateDecoder<W: Write> {
209-
inner: zio::Writer<W, DecompressError, Decompress>,
209+
inner: zio::Writer<W, Decompress>,
210210
}
211211

212212
impl<W: Write> DeflateDecoder<W> {

src/gz/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::prelude::*;
44

55
use super::{corrupt, GzBuilder, GzHeader, GzHeaderParser};
66
use crate::crc::{Crc, CrcWriter};
7-
use crate::{zio, CompressError, DecompressError};
7+
use crate::zio;
88
use crate::{Compress, Compression, Decompress, Status};
99

1010
/// A gzip streaming encoder
@@ -31,7 +31,7 @@ use crate::{Compress, Compression, Decompress, Status};
3131
/// ```
3232
#[derive(Debug)]
3333
pub struct GzEncoder<W: Write> {
34-
inner: zio::Writer<W, CompressError, Compress>,
34+
inner: zio::Writer<W, Compress>,
3535
crc: Crc,
3636
crc_bytes_written: usize,
3737
header: Vec<u8>,
@@ -209,7 +209,7 @@ impl<W: Write> Drop for GzEncoder<W> {
209209
/// ```
210210
#[derive(Debug)]
211211
pub struct GzDecoder<W: Write> {
212-
inner: zio::Writer<CrcWriter<W>, DecompressError, Decompress>,
212+
inner: zio::Writer<CrcWriter<W>, Decompress>,
213213
crc_bytes: Vec<u8>,
214214
header_parser: GzHeaderParser,
215215
}

src/zio.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use std::io;
22
use std::io::prelude::*;
3-
use std::marker::PhantomData;
43
use std::mem;
54

65
use 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>
121122
where
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();

src/zlib/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io;
22
use std::io::prelude::*;
33

4-
use crate::{zio, CompressError, DecompressError};
4+
use crate::zio;
55
use crate::{Compress, Decompress};
66

77
/// A ZLIB encoder, or compressor.
@@ -29,7 +29,7 @@ use crate::{Compress, Decompress};
2929
/// ```
3030
#[derive(Debug)]
3131
pub struct ZlibEncoder<W: Write> {
32-
inner: zio::Writer<W, CompressError, Compress>,
32+
inner: zio::Writer<W, Compress>,
3333
}
3434

3535
impl<W: Write> ZlibEncoder<W> {
@@ -216,7 +216,7 @@ impl<W: Read + Write> Read for ZlibEncoder<W> {
216216
/// ```
217217
#[derive(Debug)]
218218
pub struct ZlibDecoder<W: Write> {
219-
inner: zio::Writer<W, DecompressError, Decompress>,
219+
inner: zio::Writer<W, Decompress>,
220220
}
221221

222222
impl<W: Write> ZlibDecoder<W> {

0 commit comments

Comments
 (0)