-
Notifications
You must be signed in to change notification settings - Fork 138
Add Ptr::try_with_as_{ref,mut}
#2952
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
base: G1797ed669d2bae97562a2b84e0ac229b00d5acb7
Are you sure you want to change the base?
Add Ptr::try_with_as_{ref,mut}
#2952
Conversation
Summary of ChangesHello @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 Highlights
🧠 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 AssistThe 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
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 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
|
c6c9f72 to
a70e279
Compare
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.
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::<_, (_, (_, _))>) | ||
| }) |
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.
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))>) | ||
| }) |
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.
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`.a70e279 to
6926cbf
Compare
Ptr::try_with_{as_ref,as_mut_unchecked}Ptr::try_with_as_{ref,mut}
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
0d5e54c to
e2f523f
Compare
6926cbf to
2c0b7d1
Compare
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
A common operation is to try a fallible
Ptrconversion and thendereference the
Ptrto a&or&mutin both the success and errorcases. These functions handle the dereferencing boilerplate.
Ptr::try_with_as_{ref,mut}#2952Ptr::try_with_uncheckedsafe #2953Latest Update: v4 — Compare vs v3
📚 Full Patch History
Links show the diff between the row version and the column version.
Stacked PRs enabled by GHerrit.