async-safe-defer 0.2.0

Runtime-independent LIFO cleanup scopes for synchronous and asynchronous Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use async_safe_defer::fixed::FixedAsyncScope;
use core::pin::pin;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let mut disconnect = pin!(async { println!("disconnect") });
    let mut flush = pin!(async { println!("flush") });
    let mut scope = FixedAsyncScope::<2>::new();

    scope
        .try_defer(disconnect.as_mut())
        .expect("slot available");
    scope.try_defer(flush.as_mut()).expect("slot available");
    scope.finish().await;
}