Skip to content

Commit 49709b5

Browse files
committed
Use pin_project.
1 parent d4cf159 commit 49709b5

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

examples/complex_http_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async fn main() -> Result<()> {
114114

115115
Client::finish(outgoing_body, Some(trailers))?;
116116

117-
let response = response.get().await?;
117+
let response = response.await?;
118118

119119
// Print the response.
120120

src/http/client.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::http::response::try_from_incoming;
88
use crate::io::{self, AsyncOutputStream, AsyncPollable};
99
use crate::runtime::WaitFor;
1010
use crate::time::Duration;
11+
use pin_project_lite::pin_project;
1112
use std::future::Future;
1213
use std::pin::Pin;
1314
use std::task::{Context, Poll};
@@ -83,24 +84,35 @@ impl Client {
8384

8485
let outgoing_body = OutgoingBody::new(AsyncOutputStream::new(wasi_stream), wasi_body);
8586

86-
struct IncomingResponseFuture {
87-
subscription: WaitFor,
88-
wasi: WasiFutureIncomingResponse,
87+
pin_project! {
88+
struct IncomingResponseFuture {
89+
#[pin]
90+
subscription: Option<WaitFor>,
91+
wasi: WasiFutureIncomingResponse,
92+
}
8993
}
9094
impl Future for IncomingResponseFuture {
9195
type Output = Result<Response<IncomingBody>>;
9296

9397
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
94-
match pin_project(self.subscription).poll(cx) {
98+
let this = self.project();
99+
match this.subscription.as_pin_mut().expect("make it so").poll(cx) {
95100
Poll::Pending => Poll::Pending,
96-
Poll::Ready(response) => Poll::Ready(try_from_incoming(response)),
101+
Poll::Ready(()) => Poll::Ready(
102+
this.wasi
103+
.get()
104+
.unwrap()
105+
.unwrap()
106+
.map_err(Error::from)
107+
.and_then(try_from_incoming),
108+
),
97109
}
98110
}
99111
}
100112

101113
let subscription = AsyncPollable::new(res.subscribe()).wait_for();
102114
let future = IncomingResponseFuture {
103-
subscription,
115+
subscription: Some(subscription),
104116
wasi: res,
105117
};
106118

0 commit comments

Comments
 (0)