Skip to content

Commit 5534e22

Browse files
committed
chapter 10 finished
1 parent a2d5fe9 commit 5534e22

File tree

22 files changed

+562
-172
lines changed

22 files changed

+562
-172
lines changed

ch10/a-coroutines-variables/src/future.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// NEW
2-
use std::{thread::Thread, sync::Arc};
3-
41
use crate::runtime::Waker;
5-
// END NEW
6-
72

83
pub trait Future {
94
type Output;
10-
///////////////////////// NEW
5+
116
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output>;
127
}
138

@@ -16,6 +11,7 @@ pub enum PollState<T> {
1611
NotReady,
1712
}
1813

14+
#[allow(dead_code)]
1915
pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
2016
let futures = futures.into_iter().map(|f| (false, f)).collect();
2117
JoinAll {

ch10/a-coroutines-variables/src/http.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io::{ErrorKind, Read, Write};
22

3-
use mio::{Interest, Registry, Token};
3+
use mio::Interest;
44

55
use crate::{
66
future::PollState,
@@ -55,17 +55,13 @@ impl Future for HttpGetFuture {
5555
type Output = String;
5656

5757
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output> {
58-
// If this is first time polled, start the operation
59-
// see: https://users.rust-lang.org/t/is-it-bad-behaviour-for-a-future-or-stream-to-do-something-before-being-polled/61353
60-
// Avoid dns lookup this time
58+
6159
if self.stream.is_none() {
6260
println!("FIRST POLL - START OPERATION");
6361
self.write_request();
64-
// CHANGED
6562
let stream = self.stream.as_mut().unwrap();
6663
runtime::reactor().register(stream, Interest::READABLE, self.id);
6764
runtime::reactor().set_waker(waker, self.id);
68-
// ============
6965
}
7066

7167
let mut buff = vec![0u8; 147];
@@ -81,7 +77,6 @@ impl Future for HttpGetFuture {
8177
continue;
8278
}
8379
Err(e) if e.kind() == ErrorKind::WouldBlock => {
84-
// always store the last given Waker
8580
runtime::reactor().set_waker(waker, self.id);
8681
break PollState::NotReady;
8782
}

ch10/a-coroutines-variables/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod future;
22
mod http;
33
mod runtime;
4-
use crate::http::Http;
54
use future::{Future, PollState};
65
use runtime::Waker;
76

ch10/b-coroutines-references/src/future.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
// NEW
2-
use std::{thread::Thread, sync::Arc};
3-
41
use crate::runtime::Waker;
5-
// END NEW
6-
72

83
pub trait Future {
94
type Output;
10-
///////////////////////// NEW
115
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output>;
126
}
137

@@ -16,6 +10,7 @@ pub enum PollState<T> {
1610
NotReady,
1711
}
1812

13+
#[allow(dead_code)]
1914
pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
2015
let futures = futures.into_iter().map(|f| (false, f)).collect();
2116
JoinAll {
@@ -31,7 +26,7 @@ pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
3126

3227
impl<F: Future> Future for JoinAll<F> {
3328
type Output = String;
34-
////////////////////////// HERE
29+
3530
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output> {
3631
for (finished, fut) in self.futures.iter_mut() {
3732
if *finished {

ch10/b-coroutines-references/src/http.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::io::{ErrorKind, Read, Write};
2-
3-
use mio::{Interest, Registry, Token};
4-
51
use crate::{
62
future::PollState,
73
runtime::{self, reactor, Waker},
84
Future,
95
};
6+
use mio::Interest;
7+
use std::io::{ErrorKind, Read, Write};
108

119
fn get_req(path: &str) -> String {
1210
format!(
@@ -55,17 +53,12 @@ impl Future for HttpGetFuture {
5553
type Output = String;
5654

5755
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output> {
58-
// If this is first time polled, start the operation
59-
// see: https://users.rust-lang.org/t/is-it-bad-behaviour-for-a-future-or-stream-to-do-something-before-being-polled/61353
60-
// Avoid dns lookup this time
6156
if self.stream.is_none() {
6257
println!("FIRST POLL - START OPERATION");
6358
self.write_request();
64-
// CHANGED
6559
let stream = self.stream.as_mut().unwrap();
6660
runtime::reactor().register(stream, Interest::READABLE, self.id);
6761
runtime::reactor().set_waker(waker, self.id);
68-
// ============
6962
}
7063

7164
let mut buff = vec![0u8; 147];

ch10/b-coroutines-references/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod future;
22
mod http;
33
mod runtime;
4-
use crate::http::Http;
54
use future::{Future, PollState};
65
use runtime::Waker;
76
use std::fmt::Write;

ch10/c-coroutines-problem/src/future.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// NEW
2-
use std::{thread::Thread, sync::Arc};
3-
41
use crate::runtime::Waker;
5-
// END NEW
6-
72

83
pub trait Future {
94
type Output;
10-
///////////////////////// NEW
5+
116
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output>;
127
}
138

@@ -16,6 +11,7 @@ pub enum PollState<T> {
1611
NotReady,
1712
}
1813

14+
#[allow(dead_code)]
1915
pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
2016
let futures = futures.into_iter().map(|f| (false, f)).collect();
2117
JoinAll {

ch10/c-coroutines-problem/src/http.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use mio::Interest;
12
use std::io::{ErrorKind, Read, Write};
23

3-
use mio::{Interest, Registry, Token};
4-
54
use crate::{
65
future::PollState,
76
runtime::{self, reactor, Waker},
@@ -55,17 +54,13 @@ impl Future for HttpGetFuture {
5554
type Output = String;
5655

5756
fn poll(&mut self, waker: &Waker) -> PollState<Self::Output> {
58-
// If this is first time polled, start the operation
59-
// see: https://users.rust-lang.org/t/is-it-bad-behaviour-for-a-future-or-stream-to-do-something-before-being-polled/61353
60-
// Avoid dns lookup this time
6157
if self.stream.is_none() {
6258
println!("FIRST POLL - START OPERATION");
6359
self.write_request();
64-
// CHANGED
60+
6561
let stream = self.stream.as_mut().unwrap();
6662
runtime::reactor().register(stream, Interest::READABLE, self.id);
6763
runtime::reactor().set_waker(waker, self.id);
68-
// ============
6964
}
7065

7166
let mut buff = vec![0u8; 147];
@@ -81,7 +76,6 @@ impl Future for HttpGetFuture {
8176
continue;
8277
}
8378
Err(e) if e.kind() == ErrorKind::WouldBlock => {
84-
// always store the last given Waker
8579
runtime::reactor().set_waker(waker, self.id);
8680
break PollState::NotReady;
8781
}

ch10/c-coroutines-problem/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod future;
22
mod http;
33
mod runtime;
4-
use crate::http::Http;
54
use future::{Future, PollState};
65
use runtime::Waker;
76

ch10/d-pin/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "x-pin-experiments"
2+
name = "d-pin"
33
version = "0.1.0"
44
edition = "2021"
55

0 commit comments

Comments
 (0)