Skip to content

Commit cde4825

Browse files
committed
all examples in ch10 finished!
1 parent 1891d4f commit cde4825

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

ch10/e-coroutines-pin/src/future.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
3030

3131
impl<F: Future> Future for JoinAll<F> {
3232
type Output = String;
33-
fn poll(self: Pin<&mut Self>, waker: &Waker) -> PollState<Self::Output> {
34-
let Self { futures, finished_count} = self.get_mut();
33+
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> PollState<Self::Output> {
34+
let Self { futures, finished_count } = &mut *self;
3535
for (finished, fut) in futures.iter_mut() {
3636
if *finished {
3737
continue;
@@ -40,14 +40,14 @@ pub fn join_all<F: Future>(futures: Vec<F>) -> JoinAll<F> {
4040
match fut.as_mut().poll(waker) {
4141
PollState::Ready(_) => {
4242
*finished = true;
43-
*finished_count += 1;
43+
*finished_count += 1;
4444
}
4545

4646
PollState::NotReady => continue,
4747
}
4848
}
4949

50-
if *finished_count == futures.len() {
50+
if self.finished_count == self.futures.len() {
5151
PollState::Ready(String::new())
5252
} else {
5353
PollState::NotReady

ch10/e-coroutines-pin/src/http.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,38 @@ impl HttpGetFuture {
5454
impl Future for HttpGetFuture {
5555
type Output = String;
5656

57-
fn poll(self: Pin<&mut Self>, waker: &Waker) -> PollState<Self::Output> {
57+
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> PollState<Self::Output> {
5858
// If this is first time polled, start the operation
5959
// see: https://users.rust-lang.org/t/is-it-bad-behaviour-for-a-future-or-stream-to-do-something-before-being-polled/61353
6060
// Avoid dns lookup this time
61-
let this = self.get_mut();
61+
//let this = self.get_mut();
6262

63-
if this.stream.is_none() {
63+
let id = self.id;
64+
if self.stream.is_none() {
6465
println!("FIRST POLL - START OPERATION");
65-
this.write_request();
66+
self.write_request();
6667
// CHANGED
67-
let stream = this.stream.as_mut().unwrap();
68-
runtime::reactor().register(stream, Interest::READABLE, this.id);
69-
runtime::reactor().set_waker(waker, this.id);
68+
let stream = (&mut self).stream.as_mut().unwrap();
69+
runtime::reactor().register(stream, Interest::READABLE, id);
70+
runtime::reactor().set_waker(waker, self.id);
7071
// ============
7172
}
7273

7374
let mut buff = vec![0u8; 147];
7475
loop {
75-
match this.stream.as_mut().unwrap().read(&mut buff) {
76+
match self.stream.as_mut().unwrap().read(&mut buff) {
7677
Ok(0) => {
77-
let s = String::from_utf8_lossy(&this.buffer).to_string();
78-
runtime::reactor().deregister(this.stream.as_mut().unwrap(), this.id);
78+
let s = String::from_utf8_lossy(&self.buffer).to_string();
79+
runtime::reactor().deregister(self.stream.as_mut().unwrap(), id);
7980
break PollState::Ready(s.to_string());
8081
}
8182
Ok(n) => {
82-
this.buffer.extend(&buff[0..n]);
83+
self.buffer.extend(&buff[0..n]);
8384
continue;
8485
}
8586
Err(e) if e.kind() == ErrorKind::WouldBlock => {
8687
// always store the last given Waker
87-
runtime::reactor().set_waker(waker, this.id);
88+
runtime::reactor().set_waker(waker, self.id);
8889
break PollState::NotReady;
8990
}
9091

0 commit comments

Comments
 (0)