Expand description
A versatile and easy to use defer statement for Rust. Similar to Go’s or Zig’s defer.
This crate is compatible uses no_std
.
The default features use alloc
To disable alloc set default-features to false in cargo.toml.
This crates provides 6 macros for different use cases of deferment:
-
defer!
simple deferment. Will execute when current scope ends. -
defer_move!
same asdefer!
but moves local variables into the closure. -
defer_guard!
Returns a guard that causes execution when its scope ends.- Execution can be canceled or preempted.
-
defer_move_guard!
Same asdefer_guard!
but moves local variables into the closure. -
defer_arc!
Returns a reference counted guard than can be shared with other threads.- Execution can be canceled or preempted.
- Closure must be
Send
- Target must support Arc & AtomicBool.
- Target must support alloc
- can be disabled with
default-features=false
in Cargo.toml
-
defer_move_arc!
Same asdefer_arc!
but moves local variables into the closure.- All used local variables must be
Send
.
- All used local variables must be
§Usage
Add the dependency in your Cargo.toml
:
[dependencies]
defer-heavy = "0.1.0"
§Order of execution
Rust guarantees that the order in which the closures are dropped
(and therefore executed) are in reverse order of creation.
This means the last defer!
in the scope executes first.
Macros§
- defer
- Executes a block of code when the surrounding scope ends.
- defer_
arc - Executes a block of code when the surrounding scope ends.
- defer_
guard - Executes a block of code when the surrounding scope ends.
- defer_
move - Executes a block of code when the surrounding scope ends. This macro moves all captured variables.
- defer_
move_ arc - Executes a block of code when the surrounding scope ends.
- defer_
move_ guard - Executes a block of code when the surrounding scope ends.