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 calldrop
as it unwinds, anypanic!
in adrop
implementation will likely abort.
but that "likely" doesn't make it clear enough whether I should expect (and can handle) this scenario.