File tree 1 file changed +46
-0
lines changed 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ # async-safe-defer
2
+
3
+ Minimal async- and sync-capable ` defer ` crate with:
4
+
5
+ - ✅ async support
6
+ - ✅ no ` unsafe ` code
7
+ - ✅ ` no_std ` + ` alloc ` compatible
8
+ - ✅ optional ` no_alloc ` mode
9
+ - ✅ zero dependencies
10
+
11
+ Inspired by [ ` defer ` ] ( https://crates.io/crates/defer ) , but designed for embedded and async contexts.
12
+
13
+ ## Usage
14
+
15
+ ### Sync
16
+ ``` rust
17
+ use async_defer :: defer;
18
+
19
+ fn main () {
20
+ defer! (println! (" cleanup" ));
21
+ println! (" work" );
22
+ }
23
+ ```
24
+
25
+ ### Async
26
+ ``` rust
27
+ use async_defer :: async_scope;
28
+
29
+ async_scope! (scope , {
30
+ scope . defer (|| async { println! (" async cleanup" ) });
31
+ println! (" async work" );
32
+ }). await ;
33
+ ```
34
+
35
+ ### No-alloc
36
+ ``` rust
37
+ use async_defer :: no_alloc :: AsyncScopeNoAlloc ;
38
+
39
+ fn task () -> Pin <Box <dyn Future <Output = ()> + 'static >> {
40
+ Box :: pin (async { println! (" no_alloc" ) })
41
+ }
42
+
43
+ let mut scope = AsyncScopeNoAlloc :: <2 >:: new ();
44
+ scope . defer (task );
45
+ scope . run (). await ;
46
+ ```
You can’t perform that action at this time.
0 commit comments