ScopeGuard in kernel::types
A scope guard is a small value you create alongside a resource whose only job is to run your cleanup when it goes out of scope, no matter which way the function exits. If the work finishes successful…
Half of the bugs in C drivers live in the ladder of goto labels that unwinds half-finished setup when step four of six fails. Rust removes most of that by running Drop automatically, but sometimes you need to undo something that has no owning type yet, such as a hardware bit you just set. A scope guard is a small value that runs a closure when it goes out of scope, and that you dismiss explicitly once you know you are past the point of failure. It turns the goto ladder into code that cannot forget a step, because forgetting means not writing the dismissal.
A scope guard is a small value you create alongside a resource whose only job is to run your cleanup when it goes out of scope, no matter which way the function exits. If the work finishes successful…