@@ -4,14 +4,12 @@ mod runtime;
4
4
use crate :: http:: Http ;
5
5
use std:: fmt:: Write ;
6
6
7
-
8
7
fn main ( ) {
9
8
let mut executor = runtime:: init ( ) ;
10
9
executor. block_on ( async_main ( ) ) ;
11
10
}
12
11
13
-
14
- async fn async_main ( ) -> String {
12
+ async fn async_main ( ) {
15
13
let mut buffer = String :: from ( "\n BUFFER:\n ----\n " ) ;
16
14
let writer = & mut buffer;
17
15
println ! ( "Program starting" ) ;
@@ -21,112 +19,33 @@ async fn async_main() -> String {
21
19
writeln ! ( writer, "{txt}" ) . unwrap ( ) ;
22
20
23
21
println ! ( "{}" , buffer) ;
24
- String :: new ( )
25
22
}
26
23
27
- // // =================================
28
- // // Into this:
29
- // // =================================
30
-
31
- // fn async_main() -> impl Future<Output = String> {
32
- // Coroutine0::new()
33
- // }
24
+ // use isahc::prelude::*;
34
25
35
- // enum State0 {
36
- // Start,
37
- // Wait1(Pin<Box<dyn Future<Output = String>>>),
38
- // Wait2(Pin<Box<dyn Future<Output = String>>>),
39
- // Resolved,
40
- // }
26
+ // async fn async_main2() {
27
+ // let mut buffer = String::from("\nBUFFER:\n----\n");
28
+ // let writer = &mut buffer;
29
+ // println!("Program starting");
30
+ // let mut res = isahc::get_async("http://127.0.0.1:8080/600/HelloAsyncAwait").await.unwrap();
31
+ // let txt = res.text().await.unwrap();
32
+ // writeln!(writer, "{txt}").unwrap();
33
+ // let mut res = isahc::get_async("http://127.0.0.1:8080/400/HelloAsyncAwait").await.unwrap();
34
+ // let txt = res.text().await.unwrap();
35
+ // writeln!(writer, "{txt}").unwrap();
41
36
42
- // #[derive(Default)]
43
- // struct Stack0 {
44
- // buffer: Option<String>,
45
- // writer: Option<*mut String>,
37
+ // println!("{}", buffer);
46
38
// }
47
39
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
- // }
40
+ // async fn spawn_many() {
41
+ // for i in 0..100 {
42
+ // let delay = i * 10;
43
+ // let req = format!("http://127.0.0.1:8080/{delay}/HelloAsyncAwait{i}");
127
44
128
- // State0::Resolved => panic!("Polled a resolved future"),
129
- // }
130
- // }
45
+ // runtime::spawn(async move {
46
+ // let mut res = isahc::get_async(&req).await.unwrap();
47
+ // let txt = res.text().await.unwrap();
48
+ // println!("{txt}");
49
+ // });
131
50
// }
132
51
// }
0 commit comments