What can (and should) I do if Drop panics? Can I free other resources anyway?

1k views Asked by At

I've got a simple resource which uses MaybeUninit and unsafe for external reasons:

pub struct Resource<'a, T> {
    repr: std::cell::RefMut<'a, std::mem::MaybeUninit<T>>
}
impl<'a, T> Drop for Resource<'a, T> {
    fn drop(&mut self) {
        unsafe { // Safety: `MaybeUninit<T>` is always initialized here.
            std::ptr::drop_in_place(self.repr.as_mut_ptr());
        }
    }
}

I guess, if T::drop() panics, the RefMut gets leaked, poisoning its RefCell. How can I prevent this and would it be idiomatic to do so? The documentation states:

Given that a panic! will call drop as it unwinds, any panic! in a drop implementation will likely abort.

but that "likely" doesn't make it clear enough whether I should expect (and can handle) this scenario.

0

There are 0 answers