Skip to content

Commit 57cc859

Browse files
committed
added more examples to ch11
1 parent 3e181f1 commit 57cc859

File tree

3 files changed

+28
-108
lines changed

3 files changed

+28
-108
lines changed

ch11/a-async-await/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
mio = { version = "0.8", features = ["net", "os-poll"] }
9+
isahc = "1.7"
10+
mio = { version = "0.8", features = ["net", "os-poll"] }

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

Lines changed: 22 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ mod runtime;
44
use crate::http::Http;
55
use std::fmt::Write;
66

7-
87
fn main() {
98
let mut executor = runtime::init();
109
executor.block_on(async_main());
1110
}
1211

13-
14-
async fn async_main() -> String {
12+
async fn async_main() {
1513
let mut buffer = String::from("\nBUFFER:\n----\n");
1614
let writer = &mut buffer;
1715
println!("Program starting");
@@ -21,112 +19,33 @@ async fn async_main() -> String {
2119
writeln!(writer, "{txt}").unwrap();
2220

2321
println!("{}", buffer);
24-
String::new()
2522
}
2623

27-
// // =================================
28-
// // Into this:
29-
// // =================================
30-
31-
// fn async_main() -> impl Future<Output = String> {
32-
// Coroutine0::new()
33-
// }
24+
// use isahc::prelude::*;
3425

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();
4136

42-
// #[derive(Default)]
43-
// struct Stack0 {
44-
// buffer: Option<String>,
45-
// writer: Option<*mut String>,
37+
// println!("{}", buffer);
4638
// }
4739

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}");
12744

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+
// });
13150
// }
13251
// }

ch11/a-async-await/src/runtime/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::{
44
future::Future,
55
pin::Pin,
66
sync::{Arc, Mutex},
7-
task::{Poll, RawWaker, RawWakerVTable, Context, Waker},
7+
task::{Poll, Context, Waker},
88
thread::{self, Thread},
99
};
1010

11-
type Task = Pin<Box<dyn Future<Output = String>>>;
11+
type Task = Pin<Box<dyn Future<Output = ()>>>;
1212

1313
thread_local! {
1414
static CURRENT_EXEC: ExecutorCore = ExecutorCore::default();
@@ -23,7 +23,7 @@ struct ExecutorCore {
2323

2424
pub fn spawn<F>(future: F)
2525
where
26-
F: Future<Output = String> + 'static,
26+
F: Future<Output = ()> + 'static,
2727
{
2828
CURRENT_EXEC.with(|e| {
2929
let id = e.next_id.get();
@@ -66,7 +66,7 @@ impl Executor {
6666

6767
pub fn block_on<F>(&mut self, future: F)
6868
where
69-
F: Future<Output = String> + 'static,
69+
F: Future<Output = ()> + 'static,
7070
{
7171
// ===== OPTIMIZATION, ASSUME READY
7272
// let waker = self.get_waker(usize::MAX);

0 commit comments

Comments
 (0)