Skip to content

support generic output#10

Merged
kornelski merged 3 commits intospace-wizards:masterfrom
biryukovmaxim:master
Jun 4, 2025
Merged

support generic output#10
kornelski merged 3 commits intospace-wizards:masterfrom
biryukovmaxim:master

Conversation

@biryukovmaxim
Copy link
Copy Markdown
Contributor

@biryukovmaxim biryukovmaxim commented Jun 3, 2025

📦 PR: Generalize patch output buffer to support AlignedVec and other custom writers

✨ Summary

This PR generalizes the patch function’s output buffer parameter (new) from a fixed &mut Vec<u8> to any type that implements:

  • std::io::Write — for writing patch data,
  • DerefMut<Target = [u8]> — for in-place mutation of the diff section.

This allows the function to be used with buffer types like AlignedVec, which provide memory alignment guarantees required for later reinterpretation (e.g., casting to typed data).


🧪 What Changed

  • Signature change:
    - pub fn patch<T: Read>(..., new: &mut Vec<u8>)
    + pub fn patch<T: Read, W: Write + DerefMut<Target = [u8]>>(..., new: &mut W)

⚠️ Breaking Change: Type Inference

let mut buf = Default::default();
patch(old, &mut patch_data, &mut buf); // ❌ may fail to infer `Vec<u8>`

Developers may now need to explicitly specify the type:

let mut buf: Vec<u8> = Default::default();

✅ Benefits

  • Supports memory-aligned buffers like AlignedVec.
  • Enables more flexible and performant downstream use cases (e.g., reinterpreting as typed memory).
  • Maintains all behavior of the original patch logic.

Let me know if you want a version with a fallback function to keep compatibility for existing callers.

Comment thread src/patch.rs

/// Apply a patch to an "old" file, returning the "new" file.
///
/// `old` is the old file, `patch` will be read from with the patch,`new` is the buffer that will be written into.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR!

Please add documentation and perhaps an example that it works with a &mut Vec, because the trait bounds don't make that obvious.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@biryukovmaxim biryukovmaxim requested a review from kornelski June 3, 2025 13:56
@kornelski kornelski merged commit 5486a14 into space-wizards:master Jun 4, 2025
1 check 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