-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Improve safety for BlobVec::replace_unchecked
#7181
Conversation
crates/bevy_utils/src/lib.rs
Outdated
/// A type which calls a function when dropped. | ||
/// This can be used to ensure that cleanup code is run even in case of a panic. | ||
pub struct OnDrop<F: FnOnce()> { | ||
callback: ManuallyDrop<F>, | ||
} |
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.
The previous (private) version of this type used F: FnMut()
, which made working with owned values complicated. Since we need unsafe code in order to make this work using FnOnce()
, I figured it's worth going the extra mile and making this public.
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.
Very clever: I like this. It would be nice to see a small doc test here.
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.
Generally LGTM. There are a few points to cover though.
let old_len = self.len; | ||
let ptr = self.get_unchecked_mut(index).promote().as_ptr(); | ||
self.len = 0; |
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.
Thinking on this a bit more. This doesn't need to be done unless the drop value is Some.
Perhaps we should just branch on - drop and simplify the non-drop path.
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.
I was doubtful at first but this suggestion turned out very nicely.
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 looks so much better. LGTM.
Wondering if this saves a bit of perf on table moves.
|
||
/// A type which calls a function when dropped. | ||
/// This can be used to ensure that cleanup code is run even in case of a panic. | ||
pub struct OnDrop<F: FnOnce()> { |
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.
Btw, bevy_tasks
also has a CallOnDrop
impl which could replaced with this. Not sure if it's worth taking on bevy_utils
as a dependency.
Co-authored-by: Boxy <supbscripter@gmail.com> Co-authored-by: James Liu <contact@jamessliu.com>
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.
Much cleaner; I'm glad to see that Boxy's review comments helped.
bors r+ |
# Objective - The function `BlobVec::replace_unchecked` has informal use of safety comments. - This function does strange things with `OwningPtr` in order to get around the borrow checker. ## Solution - Put safety comments in front of each unsafe operation. Describe the specific invariants of each operation and how they apply here. - Added a guard type `OnDrop`, which is used to simplify ownership transfer in case of a panic. --- ## Changelog + Added the guard type `bevy_utils::OnDrop`. + Added conversions from `Ptr`, `PtrMut`, and `OwningPtr` to `NonNull<u8>`.
BlobVec::replace_unchecked
BlobVec::replace_unchecked
# Objective - The function `BlobVec::replace_unchecked` has informal use of safety comments. - This function does strange things with `OwningPtr` in order to get around the borrow checker. ## Solution - Put safety comments in front of each unsafe operation. Describe the specific invariants of each operation and how they apply here. - Added a guard type `OnDrop`, which is used to simplify ownership transfer in case of a panic. --- ## Changelog + Added the guard type `bevy_utils::OnDrop`. + Added conversions from `Ptr`, `PtrMut`, and `OwningPtr` to `NonNull<u8>`.
# Objective - The function `BlobVec::replace_unchecked` has informal use of safety comments. - This function does strange things with `OwningPtr` in order to get around the borrow checker. ## Solution - Put safety comments in front of each unsafe operation. Describe the specific invariants of each operation and how they apply here. - Added a guard type `OnDrop`, which is used to simplify ownership transfer in case of a panic. --- ## Changelog + Added the guard type `bevy_utils::OnDrop`. + Added conversions from `Ptr`, `PtrMut`, and `OwningPtr` to `NonNull<u8>`.
Objective
BlobVec::replace_unchecked
has informal use of safety comments.OwningPtr
in order to get around the borrow checker.Solution
OnDrop
, which is used to simplify ownership transfer in case of a panic.Changelog
bevy_utils::OnDrop
.Ptr
,PtrMut
, andOwningPtr
toNonNull<u8>
.