Skip to content

Conversation

@nabijaczleweli
Copy link
Contributor

@nabijaczleweli nabijaczleweli commented Sep 29, 2025

This doesn't affect existing callers (whatever.add_extra_data(a, b, c) where b: Box<[u8]> continues to work) but it lets new ones not heap-allocate slices which are, in reality, borrowed.

This lets the internal ExtendedFileOptions::add_extra_data_unchecked() interface take a borrowed slice and 2/3 callers are trivially upgraded to borrowing stack slices instead of allocating.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nabijaczleweli, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the add_extra_data methods within the ExtendedFileOptions and FileOptions structs to enhance flexibility and optimize memory usage. By transitioning the data parameter from Box<[u8]> to a generic AsRef<[u8]>, the change enables callers to provide borrowed slices directly, thereby eliminating unnecessary heap allocations for extra data fields. This modification improves the efficiency of how extra data is handled, particularly for fixed-size or stack-allocated data, without breaking existing API usage.

Highlights

  • API Signature Update: The add_extra_data methods in both ExtendedFileOptions and FileOptions have been updated to accept a generic type D that implements AsRef<[u8]>, moving away from the more restrictive Box<[u8]>. This change enhances flexibility for callers.
  • Internal Refactoring for Efficiency: An internal helper method, add_extra_data_impl, was introduced to encapsulate the core logic, accepting a &[u8] slice. The public add_extra_data now acts as a wrapper, converting the AsRef<[u8]> input to a slice before calling the internal implementation. The add_extra_data_unchecked function was also updated to directly work with &[u8].
  • Memory Optimization: This refactoring allows new callers to pass borrowed stack slices directly to the add_extra_data methods, eliminating unnecessary heap allocations that were previously required when using Box<[u8]>. Existing callers using Box<[u8]> remain compatible.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors [Extended]FileOptions::add_extra_data() to be more flexible and performant by accepting AsRef<[u8]> instead of Box<[u8]>. This is a great improvement as it allows callers to avoid unnecessary heap allocations, as demonstrated in the updated call sites. The changes are well-implemented and improve the library's API. I've found one minor issue that prevents compilation, which I've commented on. Once that's fixed, this should be good to merge.

Copy link
Member

@Pr0methean Pr0methean left a comment

Choose a reason for hiding this comment

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

Looks good in principle, but I have one question.

src/write.rs Outdated
) -> ZipResult<()> {
self.add_extra_data_impl(header_id, data.as_ref(), central_only)
}
fn add_extra_data_impl(
Copy link
Member

@Pr0methean Pr0methean Oct 8, 2025

Choose a reason for hiding this comment

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

Why split this method? It seems to me it shouldn't occupy more than about 1KB compiled, so the cost of inlining it into 2 or 3 generic specializations should be very reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason not to, I suppose? I can unsplit this if you want.

Copy link
Member

Choose a reason for hiding this comment

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

Yes please.

body.push(mode as u8); // strength
body.write_u16_le(underlying.serialize_to_u16())?; // real compression method
let mut body = [0; 7];
[body[0], body[1]] = (vendor as u16).to_le_bytes(); // vendor version (1 or 2)
Copy link
Member

Choose a reason for hiding this comment

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

It looks from the build errors like this may need to be body[0..=1] instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was complaining about wanting = *b"AE" actually ([body[0], body[1]] is already used elsewhere in the file); I think I didn't test with all the right feature combos locally, should all be fixed.

Pr0methean
Pr0methean previously approved these changes Oct 9, 2025
@Pr0methean Pr0methean enabled auto-merge October 9, 2025 00:51
@Pr0methean Pr0methean changed the title refactor: Upgrade [Extended]FileOptions::add_extra_data() data from Box<[u8]> to AsRef<[u8]> feat: Upgrade [Extended]FileOptions::add_extra_data() data from Box<[u8]> to AsRef<[u8]> Oct 9, 2025
@Pr0methean
Copy link
Member

Pr0methean commented Oct 9, 2025

Looks like this will break backward-compatibility, and Cargo rules say that requires a new major release. Try bumping the version to 6.0.0.

@Pr0methean Pr0methean disabled auto-merge October 9, 2025 02:16
@Pr0methean Pr0methean enabled auto-merge October 9, 2025 02:49
@Pr0methean Pr0methean disabled auto-merge October 9, 2025 03:06
@Pr0methean Pr0methean enabled auto-merge October 9, 2025 03:18
@Pr0methean Pr0methean added this pull request to the merge queue Oct 9, 2025
Merged via the queue into zip-rs:master with commit abfc23d Oct 9, 2025
65 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