1 stable release
1.0.0 | Jun 2, 2023 |
---|
#20 in #defer
6KB
63 lines
rdefer
A Rust crate providing defer functionality for both synchronous and asynchronous code.
Usage
Synchronous Defer
use rdefer::defer;
let _d = defer!({
println!("This will be printed last");
});
println!("This will be printed first");
Asynchronous Defer (Feature gated)
This feature is behind the async feature flag.
To use this feature, add rdefer to your Cargo.toml with the async feature enabled:
[dependencies]
rdefer = { version = "*", features = ["async"] }
Then you can use it as follows:
use rdefer::{async_defer, exec_before_defer};
use std::sync::Arc;
let defer = async_defer!(2, async {
println!("This will be printed last");
});
exec_before_defer!(defer, || println!("This will be printed first"));
exec_before_defer!(defer, || println!("This will be printed second"));
lib.rs
:
A Rust crate providing defer functionality for both synchronous and asynchronous code.
This crate provides a defer functionality which is similar to Go's defer
statement.
Furthermore it also allows for asynchronous defers. This is done by using a counter
which is decremented every time a defer is executed. When the counter reaches 0, the
provided function is executed.
Dependencies
~0–6MB
~21K SLoC