-
Notifications
You must be signed in to change notification settings - Fork 183
use zlib_rs api
#513
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
Open
folkertdev
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
folkertdev:zlib-rs-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−31
Open
use zlib_rs api
#513
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| //! Implementation for `zlib_rs` rust backend. | ||
| //! | ||
| //! Every backend must provide two types: | ||
| //! | ||
| //! - `Deflate` for compression, implements the `Backend` and `DeflateBackend` trait | ||
| //! - `Inflate` for decompression, implements the `Backend` and `InflateBackend` trait | ||
| //! | ||
| //! Additionally the backend provides a number of constants, and a `ErrorMessage` type. | ||
| //! | ||
| //! ## Allocation | ||
| //! | ||
| //! The (de)compression state is not boxed. The C implementations require that the z_stream is | ||
| //! pinned in memory (has a fixed address), because their z_stream is self-referential. The most | ||
| //! convenient way in rust to guarantee a stable address is to `Box` the data, but it does add an | ||
| //! additional allocation. | ||
| //! | ||
| //! With zlib_rs the state is not self-referential and hence no boxing is needed. The `new` methods | ||
| //! internally do allocate space for the (de)compression state. | ||
|
Comment on lines
+3
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lovely! This gives me the background about some fundamentals of the crate that I should have obtained years ago 😅. |
||
|
|
||
| use std::fmt; | ||
|
|
||
| use ::zlib_rs::{DeflateFlush, InflateError, InflateFlush}; | ||
|
|
||
| pub const MZ_NO_FLUSH: isize = DeflateFlush::NoFlush as isize; | ||
| pub const MZ_PARTIAL_FLUSH: isize = DeflateFlush::PartialFlush as isize; | ||
| pub const MZ_SYNC_FLUSH: isize = DeflateFlush::SyncFlush as isize; | ||
| pub const MZ_FULL_FLUSH: isize = DeflateFlush::FullFlush as isize; | ||
| pub const MZ_FINISH: isize = DeflateFlush::Finish as isize; | ||
|
|
||
| pub const MZ_DEFAULT_WINDOW_BITS: core::ffi::c_int = 15; | ||
|
|
||
| use super::*; | ||
|
|
||
| impl From<::zlib_rs::Status> for crate::mem::Status { | ||
| fn from(value: ::zlib_rs::Status) -> Self { | ||
| match value { | ||
| ::zlib_rs::Status::Ok => crate::mem::Status::Ok, | ||
| ::zlib_rs::Status::BufError => crate::mem::Status::BufError, | ||
| ::zlib_rs::Status::StreamEnd => crate::mem::Status::StreamEnd, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Default)] | ||
| pub struct ErrorMessage(Option<&'static str>); | ||
|
|
||
| impl ErrorMessage { | ||
| pub fn get(&self) -> Option<&str> { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| pub struct Inflate { | ||
| pub(crate) inner: ::zlib_rs::Inflate, | ||
| } | ||
|
|
||
| impl fmt::Debug for Inflate { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { | ||
| write!( | ||
| f, | ||
| "zlib_rs inflate internal state. total_in: {}, total_out: {}", | ||
| self.inner.total_in(), | ||
| self.inner.total_out(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl From<FlushDecompress> for DeflateFlush { | ||
| fn from(value: FlushDecompress) -> Self { | ||
| match value { | ||
| FlushDecompress::None => Self::NoFlush, | ||
| FlushDecompress::Sync => Self::SyncFlush, | ||
| FlushDecompress::Finish => Self::Finish, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl InflateBackend for Inflate { | ||
| fn make(zlib_header: bool, window_bits: u8) -> Self { | ||
| Inflate { | ||
| inner: ::zlib_rs::Inflate::new(zlib_header, window_bits), | ||
Byron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| fn decompress( | ||
| &mut self, | ||
| input: &[u8], | ||
| output: &mut [u8], | ||
| flush: FlushDecompress, | ||
| ) -> Result<Status, DecompressError> { | ||
| let flush = match flush { | ||
| FlushDecompress::None => InflateFlush::NoFlush, | ||
| FlushDecompress::Sync => InflateFlush::SyncFlush, | ||
| FlushDecompress::Finish => InflateFlush::Finish, | ||
| }; | ||
|
|
||
| match self.inner.decompress(input, output, flush) { | ||
| Ok(status) => Ok(status.into()), | ||
| Err(InflateError::NeedDict { dict_id }) => crate::mem::decompress_need_dict(dict_id), | ||
| Err(e) => crate::mem::decompress_failed(ErrorMessage(Some(e.as_str()))), | ||
| } | ||
| } | ||
|
|
||
| fn reset(&mut self, zlib_header: bool) { | ||
| self.inner.reset(zlib_header); | ||
| } | ||
| } | ||
|
|
||
| impl Backend for Inflate { | ||
| #[inline] | ||
| fn total_in(&self) -> u64 { | ||
| self.inner.total_in() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn total_out(&self) -> u64 { | ||
| self.inner.total_out() | ||
| } | ||
| } | ||
|
|
||
| pub struct Deflate { | ||
| pub(crate) inner: ::zlib_rs::Deflate, | ||
| } | ||
|
|
||
| impl fmt::Debug for Deflate { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { | ||
| write!( | ||
| f, | ||
| "zlib_rs deflate internal state. total_in: {}, total_out: {}", | ||
| self.inner.total_in(), | ||
| self.inner.total_out(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl DeflateBackend for Deflate { | ||
| fn make(level: Compression, zlib_header: bool, window_bits: u8) -> Self { | ||
| // Check in case the integer value changes at some point. | ||
| debug_assert!(level.level() <= 9); | ||
|
|
||
| Deflate { | ||
| inner: ::zlib_rs::Deflate::new(level.level() as i32, zlib_header, window_bits), | ||
| } | ||
| } | ||
|
|
||
| fn compress( | ||
| &mut self, | ||
| input: &[u8], | ||
| output: &mut [u8], | ||
| flush: FlushCompress, | ||
| ) -> Result<Status, CompressError> { | ||
| let flush = match flush { | ||
| FlushCompress::None => DeflateFlush::NoFlush, | ||
| FlushCompress::Partial => DeflateFlush::PartialFlush, | ||
| FlushCompress::Sync => DeflateFlush::SyncFlush, | ||
| FlushCompress::Full => DeflateFlush::FullFlush, | ||
| FlushCompress::Finish => DeflateFlush::Finish, | ||
| }; | ||
|
|
||
| match self.inner.compress(input, output, flush) { | ||
| Ok(status) => Ok(status.into()), | ||
| Err(e) => crate::mem::compress_failed(ErrorMessage(Some(e.as_str()))), | ||
| } | ||
| } | ||
|
|
||
| fn reset(&mut self) { | ||
| self.inner.reset(); | ||
| } | ||
| } | ||
|
|
||
| impl Backend for Deflate { | ||
| #[inline] | ||
| fn total_in(&self) -> u64 { | ||
| self.inner.total_in() | ||
| } | ||
|
|
||
| #[inline] | ||
| fn total_out(&self) -> u64 { | ||
| self.inner.total_out() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would help me here is a quick idea of why this module exports what it exports. My guess is that these types are used to integrate with
flate2, and it expects these primitives to exist and implemented by each backend.It's probably stating the obvious then 😅, and maybe there can be more in-depth information on what makes this module a little bit special even. Are there implications for allocations? Does it allocate? I guess I'd be happy to ready about everything you were thinking at the time of writing so it's conserved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote some things but yeah generally it's just the most bare-bones thing that satisfies the assumptions that are made about a backend.