Skip to content

Commit 6146cf0

Browse files
authored
Merge pull request #461 from CosminPerRam/experimental/traits
feat: add Error associated type in zio::Ops to handle multiple errors
2 parents ee1571b + 5adcf63 commit 6146cf0

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/zio.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::io;
22
use std::io::prelude::*;
33
use std::mem;
44

5-
use crate::{Compress, Decompress, DecompressError, FlushCompress, FlushDecompress, Status};
5+
use crate::{
6+
Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status,
7+
};
68

79
#[derive(Debug)]
810
pub struct Writer<W: Write, D: Ops> {
@@ -12,6 +14,7 @@ pub struct Writer<W: Write, D: Ops> {
1214
}
1315

1416
pub trait Ops {
17+
type Error: Into<io::Error>;
1518
type Flush: Flush;
1619
fn total_in(&self) -> u64;
1720
fn total_out(&self) -> u64;
@@ -20,16 +23,17 @@ pub trait Ops {
2023
input: &[u8],
2124
output: &mut [u8],
2225
flush: Self::Flush,
23-
) -> Result<Status, DecompressError>;
26+
) -> Result<Status, Self::Error>;
2427
fn run_vec(
2528
&mut self,
2629
input: &[u8],
2730
output: &mut Vec<u8>,
2831
flush: Self::Flush,
29-
) -> Result<Status, DecompressError>;
32+
) -> Result<Status, Self::Error>;
3033
}
3134

3235
impl Ops for Compress {
36+
type Error = CompressError;
3337
type Flush = FlushCompress;
3438
fn total_in(&self) -> u64 {
3539
self.total_in()
@@ -42,20 +46,21 @@ impl Ops for Compress {
4246
input: &[u8],
4347
output: &mut [u8],
4448
flush: FlushCompress,
45-
) -> Result<Status, DecompressError> {
46-
Ok(self.compress(input, output, flush).unwrap())
49+
) -> Result<Status, CompressError> {
50+
self.compress(input, output, flush)
4751
}
4852
fn run_vec(
4953
&mut self,
5054
input: &[u8],
5155
output: &mut Vec<u8>,
5256
flush: FlushCompress,
53-
) -> Result<Status, DecompressError> {
54-
Ok(self.compress_vec(input, output, flush).unwrap())
57+
) -> Result<Status, CompressError> {
58+
self.compress_vec(input, output, flush)
5559
}
5660
}
5761

5862
impl Ops for Decompress {
63+
type Error = DecompressError;
5964
type Flush = FlushDecompress;
6065
fn total_in(&self) -> u64 {
6166
self.total_in()
@@ -170,7 +175,9 @@ impl<W: Write, D: Ops> Writer<W, D> {
170175
self.dump()?;
171176

172177
let before = self.data.total_out();
173-
self.data.run_vec(&[], &mut self.buf, D::Flush::finish())?;
178+
self.data
179+
.run_vec(&[], &mut self.buf, Flush::finish())
180+
.map_err(Into::into)?;
174181
if before == self.data.total_out() {
175182
return Ok(());
176183
}
@@ -254,8 +261,8 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {
254261

255262
fn flush(&mut self) -> io::Result<()> {
256263
self.data
257-
.run_vec(&[], &mut self.buf, D::Flush::sync())
258-
.unwrap();
264+
.run_vec(&[], &mut self.buf, Flush::sync())
265+
.map_err(Into::into)?;
259266

260267
// Unfortunately miniz doesn't actually tell us when we're done with
261268
// pulling out all the data from the internal stream. To remedy this we
@@ -266,8 +273,8 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {
266273
self.dump()?;
267274
let before = self.data.total_out();
268275
self.data
269-
.run_vec(&[], &mut self.buf, D::Flush::none())
270-
.unwrap();
276+
.run_vec(&[], &mut self.buf, Flush::none())
277+
.map_err(Into::into)?;
271278
if before == self.data.total_out() {
272279
break;
273280
}

0 commit comments

Comments
 (0)