Skip to content

Commit dcac32f

Browse files
committed
add error traits
1 parent 5c6eaec commit dcac32f

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
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;
4+
use crate::{zio, CompressError, DecompressError};
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, Compress>,
31+
inner: zio::Writer<W, CompressError, 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, Decompress>,
209+
inner: zio::Writer<W, DecompressError, 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;
7+
use crate::{zio, CompressError, DecompressError};
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, Compress>,
34+
inner: zio::Writer<W, CompressError, 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>, Decompress>,
212+
inner: zio::Writer<CrcWriter<W>, DecompressError, Decompress>,
213213
crc_bytes: Vec<u8>,
214214
header_parser: GzHeaderParser,
215215
}

src/zio.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use std::io;
22
use std::io::prelude::*;
3+
use std::marker::PhantomData;
34
use 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>
119121
where
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();

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;
4+
use crate::{zio, CompressError, DecompressError};
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, Compress>,
32+
inner: zio::Writer<W, CompressError, 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, Decompress>,
219+
inner: zio::Writer<W, DecompressError, Decompress>,
220220
}
221221

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

0 commit comments

Comments
 (0)