File tree 4 files changed +70
-14
lines changed
4 files changed +70
-14
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
Original file line number Diff line number Diff line change 1
1
#![ feature( async_closure, noop_waker, async_fn_traits) ]
2
2
3
3
use std:: future:: Future ;
4
+ use std:: ops:: { AsyncFnMut , AsyncFnOnce } ;
4
5
use std:: pin:: pin;
5
6
use std:: task:: * ;
6
7
@@ -16,25 +17,36 @@ pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
16
17
}
17
18
}
18
19
19
- async fn call_once ( f : impl async FnOnce ( DropMe ) ) {
20
- f ( DropMe ( "world" ) ) . await ;
20
+ async fn call_mut ( f : & mut impl AsyncFnMut ( i32 ) ) {
21
+ f ( 0 ) . await ;
21
22
}
22
23
23
- #[ derive( Debug ) ]
24
- struct DropMe ( & ' static str ) ;
24
+ async fn call_once ( f : impl AsyncFnOnce ( i32 ) ) {
25
+ f ( 1 ) . await ;
26
+ }
25
27
26
- impl Drop for DropMe {
27
- fn drop ( & mut self ) {
28
- println ! ( "{}" , self . 0 ) ;
29
- }
28
+ async fn call_normal < F : Future < Output = ( ) > > ( f : & impl Fn ( i32 ) -> F ) {
29
+ f ( 0 ) . await ;
30
+ }
31
+
32
+ async fn call_normal_once < F : Future < Output = ( ) > > ( f : impl FnOnce ( i32 ) -> F ) {
33
+ f ( 1 ) . await ;
30
34
}
31
35
32
36
pub fn main ( ) {
33
37
block_on ( async {
34
- let b = DropMe ( "hello" ) ;
35
- let async_closure = async move |a : DropMe | {
36
- println ! ( "{a:? } {b:? }" ) ;
38
+ let b = 2i32 ;
39
+ let mut async_closure = async move |a : i32 | {
40
+ println ! ( "{a} {b}" ) ;
37
41
} ;
42
+ call_mut ( & mut async_closure) . await ;
38
43
call_once ( async_closure) . await ;
44
+
45
+ // No-capture closures implement `Fn`.
46
+ let async_closure = async move |a : i32 | {
47
+ println ! ( "{a}" ) ;
48
+ } ;
49
+ call_normal ( & async_closure) . await ;
50
+ call_normal_once ( async_closure) . await ;
39
51
} ) ;
40
52
}
Original file line number Diff line number Diff line change 1
- DropMe("world") DropMe("hello")
2
- world
3
- hello
1
+ 0 2
2
+ 1 2
3
+ 0
4
+ 1
You can’t perform that action at this time.
0 commit comments