-
-
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
Changes from 6 commits
a341e1a
fd99fc0
6b34972
0d6fa32
2d3e1bf
27bd505
542a820
f5b9267
83bebeb
0552eb4
0e97ee0
da86e26
4f03b28
49a8e18
6cfbeef
6a1af02
fd2e89d
0ba22ab
86aa368
80c6771
5baa237
0ed0b2a
c21fc09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ use std::{ | |
future::Future, | ||
hash::{BuildHasher, Hash, Hasher}, | ||
marker::PhantomData, | ||
mem::ManuallyDrop, | ||
ops::Deref, | ||
pin::Pin, | ||
}; | ||
|
@@ -233,3 +234,26 @@ impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K, | |
} | ||
} | ||
} | ||
|
||
/// 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 commentThe reason will be displayed to describe this comment to others. Learn more. Btw, |
||
callback: ManuallyDrop<F>, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous (private) version of this type used There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
impl<F: FnOnce()> OnDrop<F> { | ||
/// Returns an object that will invoke the specified callback when dropped. | ||
pub fn new(callback: F) -> Self { | ||
Self { | ||
callback: ManuallyDrop::new(callback), | ||
} | ||
} | ||
} | ||
|
||
impl<F: FnOnce()> Drop for OnDrop<F> { | ||
fn drop(&mut self) { | ||
// SAFETY: We may move out of `self`, since this instance can never be observed after it's dropped. | ||
let callback = unsafe { ManuallyDrop::take(&mut self.callback) }; | ||
callback(); | ||
} | ||
} |
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.