3 releases
Uses new Rust 2024
new 0.1.2 | Apr 12, 2025 |
---|---|
0.1.1 | Apr 12, 2025 |
0.1.0 | Apr 12, 2025 |
#1953 in Rust patterns
11KB
200 lines
This library provides two implementations of RAII-style deferred execution:
one using dynamic allocation (the default) and one that avoids allocation
entirely (no_alloc
), with a fixed-capacity array of deferred function pointers.
async-safe-defer
Minimal async- and sync-capable defer
crate with:
- ✅ async support
- ✅ no
unsafe
code - ✅
no_std
+alloc
compatible - ✅ optional
no_alloc
mode - ✅ zero dependencies
Inspired by defer
, but designed for embedded and async contexts.
Usage
Sync
use async_defer::defer;
fn main() {
defer!(println!("cleanup"));
println!("work");
}
Async
use async_defer::async_scope;
async_scope!(scope, {
scope.defer(|| async { println!("async cleanup") });
println!("async work");
}).await;
No-alloc
use async_defer::no_alloc::AsyncScopeNoAlloc;
fn task() -> Pin<Box<dyn Future<Output = ()> + 'static>> {
Box::pin(async { println!("no_alloc") })
}
let mut scope = AsyncScopeNoAlloc::<2>::new();
scope.defer(task);
scope.run().await;