Skip to content

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented Jan 29, 2026

A common operation is to try a fallible Ptr conversion and then
dereference the Ptr to a & or &mut in both the success and error
cases. These functions handle the dereferencing boilerplate.


Latest Update: v4 — Compare vs v3

📚 Full Patch History

Links show the diff between the row version and the column version.

Version v3 v2 v1 Base
v4 vs v3 vs v2 vs v1 vs Base
v3 vs v2 vs v1 vs Base
v2 vs v1 vs Base
v1 vs Base

Stacked PRs enabled by GHerrit.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @joshlf, 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 enhances the Ptr type by adding two new methods, try_with_as_ref and try_with_as_mut_unchecked. These methods provide a more ergonomic way to handle operations where a fallible Ptr conversion is immediately followed by dereferencing the resulting pointer into a safe Rust reference. The change aims to reduce manual error mapping and boilerplate code, making Ptr conversions cleaner and more efficient throughout the crate.

Highlights

  • New Ptr Conversion Methods: Introduced Ptr::try_with_as_ref and Ptr::try_with_as_mut_unchecked to simplify fallible Ptr conversions followed by immediate dereferencing into &T or &mut T references, reducing boilerplate.
  • Error Trait Integration: The TryWithError trait has been implemented for AlignmentError, ValidityError, and CastError to seamlessly integrate with the new try_with pattern for error mapping.
  • Code Refactoring and Simplification: Existing FromBytes and IntoBytes implementations, as well as utility macros and Unalign::try_deref, have been refactored to utilize the new Ptr methods, leading to more concise and consistent code.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@joshlf joshlf force-pushed the G7ef173122e7ebd6e3ce2659d432363d56972eb88 branch from c6c9f72 to a70e279 Compare January 29, 2026 23:49
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 introduces Ptr::try_with_as_ref and Ptr::try_with_as_mut_unchecked to reduce boilerplate in fallible conversions that are followed by a dereference. The new helper methods are well-implemented and their use throughout the codebase significantly simplifies the code.

My main concern is the lack of safety comments for the new unsafe blocks in src/lib.rs. I've left comments with suggestions for how to document the safety of these blocks. Once those are addressed, this looks like a great improvement.

src/lib.rs Outdated
Ptr::from_mut(source).try_with_as_mut_unchecked(|ptr| {
ptr.try_cast_into_no_leftover::<_, BecauseExclusive>(None)
.map(Ptr::recall_validity::<_, (_, (_, _))>)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This unsafe block is missing a safety comment. The safety of this block depends on upholding the contract of try_with_as_mut_unchecked. Please add a comment explaining why this is safe.

        // SAFETY: The closure passed to `try_with_as_mut_unchecked` does not
        // clone the `Ptr` it receives. If the closure returns an error, the
        // `Ptr` is contained within that error, and so no copies of the `Ptr`
        // exist outside of the error. This upholds the safety contract of
        // `try_with_as_mut_unchecked`.

src/lib.rs Outdated
Ptr::from_mut(source).try_with_as_mut_unchecked(|ptr| {
ptr.try_cast_into_no_leftover::<_, BecauseImmutable>(Some(count))
.map(Ptr::recall_validity::<_, (_, (_, BecauseExclusive))>)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This unsafe block is also missing a safety comment. The reasoning is the same as for mut_from_bytes. Please add a comment explaining why this is safe.

        // SAFETY: The closure passed to `try_with_as_mut_unchecked` does not
        // clone the `Ptr` it receives. If the closure returns an error, the
        // `Ptr` is contained within that error, and so no copies of the `Ptr`
        // exist outside of the error. This upholds the safety contract of
        // `try_with_as_mut_unchecked`.

@joshlf joshlf force-pushed the G7ef173122e7ebd6e3ce2659d432363d56972eb88 branch from a70e279 to 6926cbf Compare January 30, 2026 04:59
@joshlf joshlf changed the title Add Ptr::try_with_{as_ref,as_mut_unchecked} Add Ptr::try_with_as_{ref,mut} Jan 30, 2026
@joshlf joshlf changed the base branch from main to G1797ed669d2bae97562a2b84e0ac229b00d5acb7 January 30, 2026 04:59
A common operation is to try a fallible `Ptr` conversion and then
dereference the `Ptr` to a `&` or `&mut` in both the success and error
cases. These functions handle the dereferencing boilerplate.

gherrit-pr-id: G7ef173122e7ebd6e3ce2659d432363d56972eb88
@joshlf joshlf force-pushed the G1797ed669d2bae97562a2b84e0ac229b00d5acb7 branch from 0d5e54c to e2f523f Compare January 30, 2026 05:02
@joshlf joshlf force-pushed the G7ef173122e7ebd6e3ce2659d432363d56972eb88 branch from 6926cbf to 2c0b7d1 Compare January 30, 2026 05:02
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 98.50746% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.35%. Comparing base (e2f523f) to head (2c0b7d1).

Files with missing lines Patch % Lines
src/split_at.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@                              Coverage Diff                              @@
##           G1797ed669d2bae97562a2b84e0ac229b00d5acb7    #2952      +/-   ##
=============================================================================
+ Coverage                                      92.28%   92.35%   +0.07%     
=============================================================================
  Files                                             19       19              
  Lines                                           5861     5890      +29     
=============================================================================
+ Hits                                            5409     5440      +31     
+ Misses                                           452      450       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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