File tree Expand file tree Collapse file tree 2 files changed +61
-14
lines changed Expand file tree Collapse file tree 2 files changed +61
-14
lines changed Original file line number Diff line number Diff line change 1+ #![ feature(
2+ async_await,
3+ await_macro,
4+ futures_api,
5+ pin,
6+ ) ]
7+
8+ use std:: { future:: Future , pin:: Pin , task:: Poll } ;
9+
10+ // See if we can run a basic `async fn`
11+ pub async fn foo ( x : & u32 , y : u32 ) -> u32 {
12+ let y = & y;
13+ let z = 9 ;
14+ let z = & z;
15+ let y = await ! ( async { * y + * z } ) ;
16+ let a = 10 ;
17+ let a = & a;
18+ * x + y + * a
19+ }
20+
21+ fn main ( ) {
22+ use std:: { sync:: Arc , task:: { Wake , local_waker} } ;
23+
24+ struct NoWake ;
25+ impl Wake for NoWake {
26+ fn wake ( _arc_self : & Arc < Self > ) {
27+ panic ! ( ) ;
28+ }
29+ }
30+
31+ let lw = unsafe { local_waker ( Arc :: new ( NoWake ) ) } ;
32+ let x = 5 ;
33+ let mut fut = foo ( & x, 7 ) ;
34+ assert_eq ! ( unsafe { Pin :: new_unchecked( & mut fut) } . poll( & lw) , Poll :: Ready ( 31 ) ) ;
35+ }
Original file line number Diff line number Diff line change 1313use std:: ops:: { GeneratorState , Generator } ;
1414
1515fn finish < T > ( mut amt : usize , mut t : T ) -> T :: Return
16- where T : Generator < Yield = ( ) >
16+ where T : Generator < Yield = usize >
1717{
1818 loop {
1919 match unsafe { t. resume ( ) } {
20- GeneratorState :: Yielded ( ( ) ) => amt -= 1 ,
20+ GeneratorState :: Yielded ( y ) => amt -= y ,
2121 GeneratorState :: Complete ( ret) => {
2222 assert_eq ! ( amt, 0 ) ;
2323 return ret
@@ -28,38 +28,50 @@ fn finish<T>(mut amt: usize, mut t: T) -> T::Return
2828}
2929
3030fn main ( ) {
31- finish ( 1 , || yield ) ;
31+ finish ( 1 , || yield 1 ) ;
3232 finish ( 3 , || {
3333 let mut x = 0 ;
34- yield ;
34+ yield 1 ;
3535 x += 1 ;
36- yield ;
36+ yield 1 ;
3737 x += 1 ;
38- yield ;
38+ yield 1 ;
3939 assert_eq ! ( x, 2 ) ;
4040 } ) ;
41- finish ( 8 , || {
42- for _ in 0 ..8 {
43- yield ;
41+ finish ( 7 * 8 / 2 , || {
42+ for i in 0 ..8 {
43+ yield i ;
4444 }
4545 } ) ;
4646 finish ( 1 , || {
4747 if true {
48- yield ;
48+ yield 1 ;
4949 } else {
5050 }
5151 } ) ;
5252 finish ( 1 , || {
5353 if false {
5454 } else {
55- yield ;
55+ yield 1 ;
5656 }
5757 } ) ;
5858 finish ( 2 , || {
59- if { yield ; false } {
60- yield ;
59+ if { yield 1 ; false } {
60+ yield 1 ;
6161 panic ! ( )
6262 }
63- yield
63+ yield 1 ;
6464 } ) ;
65+ // also test a self-referential generator
66+ assert_eq ! (
67+ finish( 5 , || {
68+ let mut x = Box :: new( 5 ) ;
69+ let y = & mut * x;
70+ * y = 5 ;
71+ yield * y;
72+ * y = 10 ;
73+ * x
74+ } ) ,
75+ 10
76+ ) ;
6577}
You can’t perform that action at this time.
0 commit comments