@@ -2,133 +2,131 @@ mod future;
2
2
mod http;
3
3
mod runtime;
4
4
use crate :: http:: Http ;
5
+ use std:: fmt:: Write ;
5
6
6
7
7
8
fn main ( ) {
8
9
let mut executor = runtime:: init ( ) ;
9
10
executor. block_on ( async_main ( ) ) ;
10
11
}
11
12
12
- use std:: { fmt:: Write , future:: Future , marker:: PhantomPinned , mem, pin:: Pin , task:: { Poll , Context } } ;
13
- // =================================
14
- // We rewrite this:
15
- // =================================
16
-
17
- // coro fn async_main() {
18
- // let mut buffer = String::from("\nBUFFER:\n----\n");
19
- // let writer = &mut buffer;
20
- // println!("Program starting");
21
- // let txt = http::Http::get("/600/HelloAsyncAwait").wait;
22
- // writeln!(writer, "{txt}").unwrap();
23
- // let txt = http::Http::get("/400/HelloAsyncAwait").wait;
24
- // writeln!(writer, "{txt}").unwrap();
25
- //
26
- // println!("{}", buffer);
27
- // }
28
13
29
- // =================================
30
- // Into this:
31
- // =================================
14
+ async fn async_main ( ) -> String {
15
+ let mut buffer = String :: from ( "\n BUFFER:\n ----\n " ) ;
16
+ let writer = & mut buffer;
17
+ println ! ( "Program starting" ) ;
18
+ let txt = Http :: get ( "/600/HelloAsyncAwait" ) . await ;
19
+ writeln ! ( writer, "{txt}" ) . unwrap ( ) ;
20
+ let txt = Http :: get ( "/400/HelloAsyncAwait" ) . await ;
21
+ writeln ! ( writer, "{txt}" ) . unwrap ( ) ;
32
22
33
- fn async_main ( ) -> impl Future < Output = String > {
34
- Coroutine0 :: new ( )
23
+ println ! ( "{}" , buffer ) ;
24
+ String :: new ( )
35
25
}
36
26
37
- enum State0 {
38
- Start ,
39
- Wait1 ( Pin < Box < dyn Future < Output = String > > > ) ,
40
- Wait2 ( Pin < Box < dyn Future < Output = String > > > ) ,
41
- Resolved ,
42
- }
27
+ // // =================================
28
+ // // Into this:
29
+ // // =================================
43
30
44
- #[ derive( Default ) ]
45
- struct Stack0 {
46
- buffer : Option < String > ,
47
- writer : Option < * mut String > ,
48
- }
31
+ // fn async_main() -> impl Future<Output = String> {
32
+ // Coroutine0::new()
33
+ // }
49
34
50
- struct Coroutine0 {
51
- stack : Stack0 ,
52
- state : State0 ,
53
- _pin : PhantomPinned ,
54
- }
35
+ // enum State0 {
36
+ // Start,
37
+ // Wait1(Pin<Box<dyn Future<Output = String>>>),
38
+ // Wait2(Pin<Box<dyn Future<Output = String>>>),
39
+ // Resolved,
40
+ // }
55
41
56
- impl Coroutine0 {
57
- fn new ( ) -> Self {
58
- Self {
59
- state : State0 :: Start ,
60
- stack : Stack0 :: default ( ) ,
61
- _pin : PhantomPinned ,
62
- }
63
- }
64
- }
42
+ // #[derive(Default)]
43
+ // struct Stack0 {
44
+ // buffer: Option<String>,
45
+ // writer: Option<*mut String>,
46
+ // }
65
47
66
- impl Future for Coroutine0 {
67
- type Output = String ;
68
-
69
- fn poll ( self : Pin < & mut Self > , waker : & mut Context ) -> Poll < Self :: Output > {
70
- let this = unsafe { self . get_unchecked_mut ( ) } ;
71
- loop {
72
- match this. state {
73
- State0 :: Start => {
74
- // initialize stack (hoist declarations - no stack yet)
75
-
76
- this. stack . buffer = Some ( String :: from ( "\n BUFFER:\n ----\n " ) ) ;
77
- this. stack . writer = Some ( this. stack . buffer . as_mut ( ) . unwrap ( ) ) ;
78
- // ---- Code you actually wrote ----
79
- println ! ( "Program starting" ) ;
80
-
81
- // ---------------------------------
82
- let fut1 = Box :: pin ( http:: Http :: get ( "/600/HelloAsyncAwait" ) ) ;
83
- this. state = State0 :: Wait1 ( fut1) ;
84
-
85
- // save stack
86
- // nothing to save
87
- }
88
-
89
- State0 :: Wait1 ( ref mut f1) => {
90
- match f1. as_mut ( ) . poll ( waker) {
91
- Poll :: Ready ( txt) => {
92
- // Restore stack
93
- let writer = unsafe { & mut * this. stack . writer . unwrap ( ) } ;
94
-
95
- // ---- Code you actually wrote ----
96
- writeln ! ( writer, "{txt}" ) . unwrap ( ) ;
97
- // ---------------------------------
98
- let fut2 = Box :: pin ( http:: Http :: get ( "/400/HelloAsyncAwait" ) ) ;
99
- this. state = State0 :: Wait2 ( fut2) ;
100
-
101
- // save stack
102
- this. stack . writer = Some ( writer) ;
103
- }
104
- Poll :: Pending => break Poll :: Pending ,
105
- }
106
- }
107
-
108
- State0 :: Wait2 ( ref mut f2) => {
109
- match f2. as_mut ( ) . poll ( waker) {
110
- Poll :: Ready ( txt) => {
111
- // Restore stack
112
- let buffer = this. stack . buffer . as_ref ( ) . take ( ) . unwrap ( ) ;
113
- let writer = unsafe { & mut * this. stack . writer . unwrap ( ) } ;
114
-
115
- // ---- Code you actually wrote ----
116
- writeln ! ( writer, "{txt}" ) . unwrap ( ) ;
117
-
118
- println ! ( "{}" , buffer) ;
119
- // ---------------------------------
120
- this. state = State0 :: Resolved ;
121
-
122
- // Save stack (all variables set to None already)
123
-
124
- break Poll :: Ready ( String :: new ( ) ) ;
125
- }
126
- Poll :: Pending => break Poll :: Pending ,
127
- }
128
- }
129
-
130
- State0 :: Resolved => panic ! ( "Polled a resolved future" ) ,
131
- }
132
- }
133
- }
134
- }
48
+ // struct Coroutine0 {
49
+ // stack: Stack0,
50
+ // state: State0,
51
+ // _pin: PhantomPinned,
52
+ // }
53
+
54
+ // impl Coroutine0 {
55
+ // fn new() -> Self {
56
+ // Self {
57
+ // state: State0::Start,
58
+ // stack: Stack0::default(),
59
+ // _pin: PhantomPinned,
60
+ // }
61
+ // }
62
+ // }
63
+
64
+ // impl Future for Coroutine0 {
65
+ // type Output = String;
66
+
67
+ // fn poll(self: Pin<&mut Self>, waker: &mut Context) -> Poll<Self::Output> {
68
+ // let this = unsafe { self.get_unchecked_mut() };
69
+ // loop {
70
+ // match this.state {
71
+ // State0::Start => {
72
+ // // initialize stack (hoist declarations - no stack yet)
73
+
74
+ // this.stack.buffer = Some(String::from("\nBUFFER:\n----\n"));
75
+ // this.stack.writer = Some(this.stack.buffer.as_mut().unwrap());
76
+ // // ---- Code you actually wrote ----
77
+ // println!("Program starting");
78
+
79
+ // // ---------------------------------
80
+ // let fut1 = Box::pin(http::Http::get("/600/HelloAsyncAwait"));
81
+ // this.state = State0::Wait1(fut1);
82
+
83
+ // // save stack
84
+ // // nothing to save
85
+ // }
86
+
87
+ // State0::Wait1(ref mut f1) => {
88
+ // match f1.as_mut().poll(waker) {
89
+ // Poll::Ready(txt) => {
90
+ // // Restore stack
91
+ // let writer = unsafe { &mut *this.stack.writer.unwrap() };
92
+
93
+ // // ---- Code you actually wrote ----
94
+ // writeln!(writer, "{txt}").unwrap();
95
+ // // ---------------------------------
96
+ // let fut2 = Box::pin(http::Http::get("/400/HelloAsyncAwait"));
97
+ // this.state = State0::Wait2(fut2);
98
+
99
+ // // save stack
100
+ // this.stack.writer = Some(writer);
101
+ // }
102
+ // Poll::Pending => break Poll::Pending,
103
+ // }
104
+ // }
105
+
106
+ // State0::Wait2(ref mut f2) => {
107
+ // match f2.as_mut().poll(waker) {
108
+ // Poll::Ready(txt) => {
109
+ // // Restore stack
110
+ // let buffer = this.stack.buffer.as_ref().take().unwrap();
111
+ // let writer = unsafe { &mut *this.stack.writer.unwrap() };
112
+
113
+ // // ---- Code you actually wrote ----
114
+ // writeln!(writer, "{txt}").unwrap();
115
+
116
+ // println!("{}", buffer);
117
+ // // ---------------------------------
118
+ // this.state = State0::Resolved;
119
+
120
+ // // Save stack (all variables set to None already)
121
+
122
+ // break Poll::Ready(String::new());
123
+ // }
124
+ // Poll::Pending => break Poll::Pending,
125
+ // }
126
+ // }
127
+
128
+ // State0::Resolved => panic!("Polled a resolved future"),
129
+ // }
130
+ // }
131
+ // }
132
+ // }
0 commit comments