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

Conversation

CosminPerRam
Copy link
Contributor

@CosminPerRam CosminPerRam commented Feb 13, 2025

Ops::run and Ops::run_vec return Result<Status, DecompressError>.
There are 2 implementations of it, in Decompress which everything is all good, and in Compress, which because of the trait, results in the usage of some .unwrap()'s:

flate2-rs/src/zio.rs

Lines 40 to 47 in 5c6eaec

fn run(
&mut self,
input: &[u8],
output: &mut [u8],
flush: FlushCompress,
) -> Result<Status, DecompressError> {
Ok(self.compress(input, output, flush).unwrap())
}

and

flate2-rs/src/zio.rs

Lines 48 to 55 in 5c6eaec

fn run_vec(
&mut self,
input: &[u8],
output: &mut Vec<u8>,
flush: FlushCompress,
) -> Result<Status, DecompressError> {
Ok(self.compress_vec(input, output, flush).unwrap())
}

So, adding an associated type in Ops covers both.
Because of this, these unwraps are also handled now:

flate2-rs/src/zio.rs

Lines 255 to 277 in 5c6eaec

fn flush(&mut self) -> io::Result<()> {
self.data
.run_vec(&[], &mut self.buf, D::Flush::sync())
.unwrap();
// 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
// have to continually ask the stream for more memory until it doesn't
// give us a chunk of memory the same size as our own internal buffer,
// at which point we assume it's reached the end.
loop {
self.dump()?;
let before = self.data.total_out();
self.data
.run_vec(&[], &mut self.buf, D::Flush::none())
.unwrap();
if before == self.data.total_out() {
break;
}
}
self.obj.as_mut().unwrap().flush()
}

(without the last one, that's here for context)
The trait is used only internally, so no breaking changes and only less possible panics (:

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great catch, along with a great solution!
Thanks so much!

@Byron Byron merged commit 6146cf0 into rust-lang:main Feb 18, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants