Skip to content

Commit 3e181f1

Browse files
committed
switched to async/await
1 parent 0ecc8cd commit 3e181f1

File tree

2 files changed

+115
-120
lines changed

2 files changed

+115
-120
lines changed

ch11/a-async-await/src/http.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use std::{
22
io::{ErrorKind, Read, Write},
33
pin::Pin,
4-
task::{Poll, Context},
4+
task::{Poll, Context}, future::Future,
55
};
66

77
use mio::Interest;
88

9-
use crate::{
10-
runtime::{self, reactor},
11-
Future,
12-
};
9+
use crate::runtime::{self, reactor};
1310

1411
fn get_req(path: &str) -> String {
1512
format!(

ch11/a-async-await/src/main.rs

Lines changed: 113 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,131 @@ mod future;
22
mod http;
33
mod runtime;
44
use crate::http::Http;
5+
use std::fmt::Write;
56

67

78
fn main() {
89
let mut executor = runtime::init();
910
executor.block_on(async_main());
1011
}
1112

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-
// }
2813

29-
// =================================
30-
// Into this:
31-
// =================================
14+
async fn async_main() -> String {
15+
let mut buffer = String::from("\nBUFFER:\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();
3222

33-
fn async_main() -> impl Future<Output = String> {
34-
Coroutine0::new()
23+
println!("{}", buffer);
24+
String::new()
3525
}
3626

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+
// // =================================
4330

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+
// }
4934

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+
// }
5541

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+
// }
6547

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("\nBUFFER:\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

Comments
 (0)