1
1
#![ feature( never_type) ]
2
2
3
- use std:: { future:: Future , pin:: Pin , task:: Poll } ;
4
- use std:: task:: { Wake , Waker , Context } ;
5
- use std:: sync:: Arc ;
3
+ use std:: future:: Future ;
6
4
7
5
// See if we can run a basic `async fn`
8
6
pub async fn foo ( x : & u32 , y : u32 ) -> u32 {
@@ -47,7 +45,10 @@ async fn partial_init(x: u32) -> u32 {
47
45
let _x: ( String , !) = ( String :: new ( ) , return async { x + x } . await ) ;
48
46
}
49
47
50
- fn run_fut ( mut fut : impl Future < Output =u32 > , output : u32 ) {
48
+ fn run_fut < T > ( fut : impl Future < Output = T > ) -> T {
49
+ use std:: sync:: Arc ;
50
+ use std:: task:: { Context , Poll , Wake , Waker } ;
51
+
51
52
struct MyWaker ;
52
53
impl Wake for MyWaker {
53
54
fn wake ( self : Arc < Self > ) {
@@ -57,16 +58,20 @@ fn run_fut(mut fut: impl Future<Output=u32>, output: u32) {
57
58
58
59
let waker = Waker :: from ( Arc :: new ( MyWaker ) ) ;
59
60
let mut context = Context :: from_waker ( & waker) ;
60
- assert_eq ! ( unsafe { Pin :: new_unchecked( & mut fut) } . poll( & mut context) , Poll :: Ready ( output) ) ;
61
+
62
+ let mut pinned = Box :: pin ( fut) ;
63
+ loop {
64
+ match pinned. as_mut ( ) . poll ( & mut context) {
65
+ Poll :: Pending => continue ,
66
+ Poll :: Ready ( v) => return v,
67
+ }
68
+ }
61
69
}
62
70
63
71
fn main ( ) {
64
72
let x = 5 ;
65
- run_fut ( foo ( & x, 7 ) , 31 ) ;
66
-
67
- run_fut ( build_aggregate ( 1 , 2 , 3 , 4 ) , 10 ) ;
68
-
69
- run_fut ( includes_never ( false , 4 ) , 16 ) ;
70
-
71
- run_fut ( partial_init ( 4 ) , 8 ) ;
73
+ assert_eq ! ( run_fut( foo( & x, 7 ) ) , 31 ) ;
74
+ assert_eq ! ( run_fut( build_aggregate( 1 , 2 , 3 , 4 ) ) , 10 ) ;
75
+ assert_eq ! ( run_fut( includes_never( false , 4 ) ) , 16 ) ;
76
+ assert_eq ! ( run_fut( partial_init( 4 ) ) , 8 ) ;
72
77
}
0 commit comments