Add and use OnDrop::disable

This commit is contained in:
John Kåre Alsaker 2018-05-27 07:47:44 +02:00
parent 77259af56a
commit 090b8341bc
2 changed files with 9 additions and 1 deletions

View file

@ -80,6 +80,14 @@ pub mod sorted_map;
pub struct OnDrop<F: Fn()>(pub F);
impl<F: Fn()> OnDrop<F> {
/// Forgets the function which prevents it from running.
/// Ensure that the function owns no memory, otherwise it will be leaked.
pub fn disable(self) {
std::mem::forget(self);
}
}
impl<F: Fn()> Drop for OnDrop<F> {
fn drop(&mut self) {
(self.0)();