Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Error associated type in zio::Ops to handle multiple errors #461

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/zio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::io;
use std::io::prelude::*;
use std::mem;

use crate::{Compress, Decompress, DecompressError, FlushCompress, FlushDecompress, Status};
use crate::{
Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status,
};

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

pub trait Ops {
type Error: Into<io::Error>;
type Flush: Flush;
fn total_in(&self) -> u64;
fn total_out(&self) -> u64;
Expand All @@ -20,16 +23,17 @@ pub trait Ops {
input: &[u8],
output: &mut [u8],
flush: Self::Flush,
) -> Result<Status, DecompressError>;
) -> Result<Status, Self::Error>;
fn run_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
flush: Self::Flush,
) -> Result<Status, DecompressError>;
) -> Result<Status, Self::Error>;
}

impl Ops for Compress {
type Error = CompressError;
type Flush = FlushCompress;
fn total_in(&self) -> u64 {
self.total_in()
Expand All @@ -42,20 +46,21 @@ impl Ops for Compress {
input: &[u8],
output: &mut [u8],
flush: FlushCompress,
) -> Result<Status, DecompressError> {
Ok(self.compress(input, output, flush).unwrap())
) -> Result<Status, CompressError> {
self.compress(input, output, flush)
}
fn run_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
flush: FlushCompress,
) -> Result<Status, DecompressError> {
Ok(self.compress_vec(input, output, flush).unwrap())
) -> Result<Status, CompressError> {
self.compress_vec(input, output, flush)
}
}

impl Ops for Decompress {
type Error = DecompressError;
type Flush = FlushDecompress;
fn total_in(&self) -> u64 {
self.total_in()
Expand Down Expand Up @@ -170,7 +175,9 @@ impl<W: Write, D: Ops> Writer<W, D> {
self.dump()?;

let before = self.data.total_out();
self.data.run_vec(&[], &mut self.buf, D::Flush::finish())?;
self.data
.run_vec(&[], &mut self.buf, Flush::finish())
.map_err(Into::into)?;
if before == self.data.total_out() {
return Ok(());
}
Expand Down Expand Up @@ -254,8 +261,8 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {

fn flush(&mut self) -> io::Result<()> {
self.data
.run_vec(&[], &mut self.buf, D::Flush::sync())
.unwrap();
.run_vec(&[], &mut self.buf, Flush::sync())
.map_err(Into::into)?;

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