Skip to main content

defer_drop

Macro defer_drop 

Source
macro_rules! defer_drop {
    ($value:ident, $ctx:ident) => { ... };
}
Available on crate features code and embedded-python only.
Expand description

The preferred way to ensure a [DropWithContext] value is cleaned up on every code path.

Creates a [DropGuard] and immediately rebinds $value as &V and $ctx as &mut C via [DropGuard::as_parts]. The original owned value is moved into the guard, which will call [DropWithContext::drop_with] when scope exits — whether that’s normal completion, early return via ?, continue, or any other branch.

Beyond safety, this is often much more concise than inserting drop_with calls in every branch of complex control flow. For mutable access to the value, use [defer_drop_mut!].

§Limitation

The macro rebinds $ctx as a new let binding, so it cannot be used when $ctx is self. In &mut self methods, first assign let this = self; and pass this.