ImpureTryDrop

Trait ImpureTryDrop 

Source
pub trait ImpureTryDrop {
    type Error: Into<Error>;

    // Required method
    unsafe fn try_drop(&mut self) -> Result<(), Self::Error>;
}
Expand description

A trait for types which can be dropped, but which may fail to do so.

This is an impure version of try drop, meaning that it depends on the global try drop strategy.

§Gotchas

Implementing this trait is not enough to make it droppable. In order for the try drop strategy to be run, you need to put your type in a DropAdapter.

An easier way to make your type droppable is to call PureTryDrop::adapt on it.

Required Associated Types§

Source

type Error: Into<Error>

The type of the error that may occur during drop.

Required Methods§

Source

unsafe fn try_drop(&mut self) -> Result<(), Self::Error>

Execute the fallible destructor for this type. This function is unsafe because if this is called outside of a Drop::drop context, once the scope of the object implementing trait ends, this function will be called twice, potentially resulting in a double-free.

Use DropAdapter to ensure that the destructor is only called once.

§Safety

The caller must ensure that this function is called within a Drop::drop context.

If the implementing type implements RepeatableTryDrop, however, then this function is safe to call multiple times. If the unsafe seems ugly to you, you can use RepeatableTryDrop::safe_try_drop.

Implementors§