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

Export all headers from MultiGzDecoder #348

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions src/gz/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub struct GzDecoder<R> {
state: GzState,
reader: CrcReader<deflate::bufread::DeflateDecoder<R>>,
multi: bool,
headers: Vec<GzHeader>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -240,6 +241,7 @@ impl<R: BufRead> GzDecoder<R> {
state,
reader: CrcReader::new(deflate::bufread::DeflateDecoder::new(r)),
multi: false,
headers: Vec::new(),
}
}

Expand Down Expand Up @@ -284,6 +286,7 @@ impl<R: BufRead> Read for GzDecoder<R> {
state,
reader,
multi,
headers,
} = self;

loop {
Expand Down Expand Up @@ -366,9 +369,10 @@ impl<R: BufRead> Read for GzDecoder<R> {
return Err(err);
}
};
headers.push(header);
Copy link
Member

Choose a reason for hiding this comment

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

Even if performance wouldn't be an issue, these headers contain a few allocations themselves and I can imagine that archives with millions of members would now see a problem with memory consumption where previously they would not have an issue.

In order to prevent regression in that regard, I think this feature must be opt-in, controllable from the MutliGzDecoder which already sets the multi flag.


if is_eof {
GzState::End(Some(header))
GzState::End(None)
} else {
reader.reset();
reader.get_mut().reset_data();
Expand Down Expand Up @@ -449,9 +453,13 @@ impl<R: BufRead> MultiGzDecoder<R> {
}

impl<R> MultiGzDecoder<R> {
/// Returns the current header associated with this stream, if it's valid
pub fn header(&self) -> Option<&GzHeader> {
Copy link
Member

Choose a reason for hiding this comment

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

I think there is no need to make this a breaking change by removing the single-header access method. It can live side-by-side with the headers() method.

self.0.header()
/// Returns the headers processed so far
pub fn headers(&self) -> Vec<&GzHeader> {
Copy link
Member

Choose a reason for hiding this comment

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

This could be allocation-free by returning impl Iterator<Item = &GzHeader> with this implementation:

        self.0.headers.iter().chain(self.0.header())

let mut hdrs: Vec<&GzHeader> = self.0.headers.iter().collect();
if let Some(hdr) = &self.0.header() {
hdrs.push(hdr)
}
hdrs
}

/// Acquires a reference to the underlying reader.
Expand Down
6 changes: 3 additions & 3 deletions src/gz/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ impl<R: Read> MultiGzDecoder<R> {
}

impl<R> MultiGzDecoder<R> {
/// Returns the current header associated with this stream, if it's valid.
pub fn header(&self) -> Option<&GzHeader> {
Copy link
Member

Choose a reason for hiding this comment

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

I think there is no need to make this a breaking change by removing the single-header access method. It can live side-by-side with the headers() method.

self.inner.header()
/// Returns the headers processed so far
pub fn headers(&self) -> Vec<&GzHeader> {
self.inner.headers()
}

/// Acquires a reference to the underlying reader.
Expand Down