File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ #![ feature( async_closure, noop_waker, async_fn_traits) ]
2
+
3
+ use std:: future:: Future ;
4
+ use std:: pin:: pin;
5
+ use std:: task:: * ;
6
+
7
+ pub fn block_on < T > ( fut : impl Future < Output = T > ) -> T {
8
+ let mut fut = pin ! ( fut) ;
9
+ let ctx = & mut Context :: from_waker ( Waker :: noop ( ) ) ;
10
+
11
+ loop {
12
+ match fut. as_mut ( ) . poll ( ctx) {
13
+ Poll :: Pending => { }
14
+ Poll :: Ready ( t) => break t,
15
+ }
16
+ }
17
+ }
18
+
19
+ async fn call_once ( f : impl async FnOnce ( DropMe ) ) {
20
+ f ( DropMe ( "world" ) ) . await ;
21
+ }
22
+
23
+ #[ derive( Debug ) ]
24
+ struct DropMe ( & ' static str ) ;
25
+
26
+ impl Drop for DropMe {
27
+ fn drop ( & mut self ) {
28
+ println ! ( "{}" , self . 0 ) ;
29
+ }
30
+ }
31
+
32
+ pub fn main ( ) {
33
+ block_on ( async {
34
+ let b = DropMe ( "hello" ) ;
35
+ let async_closure = async move |a : DropMe | {
36
+ println ! ( "{a:?} {b:?}" ) ;
37
+ } ;
38
+ call_once ( async_closure) . await ;
39
+ } ) ;
40
+ }
Original file line number Diff line number Diff line change
1
+ DropMe("world") DropMe("hello")
2
+ world
3
+ hello
You can’t perform that action at this time.
0 commit comments