Skip to content

Commit 9e438c0

Browse files
committed
Client, RB: remove BoxFuture from .send()
The pinned future stored inside RequestBuilder for `.poll()` must be `'static` for _reasons_. This moves the public facing `.send()` apis to use async functions, which instead return an `impl Future` on the stack.
1 parent 0cc038a commit 9e438c0

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/client.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::middleware::{Middleware, Next};
66
use crate::{HttpClient, Request, RequestBuilder, Response, Result};
77

88
use cfg_if::cfg_if;
9-
use futures_util::future::BoxFuture;
109

1110
cfg_if! {
1211
if #[cfg(feature = "curl-client")] {
@@ -182,23 +181,22 @@ impl Client {
182181
/// let res = client.send(req).await?;
183182
/// # Ok(()) }
184183
/// ```
185-
pub fn send(&self, req: impl Into<Request>) -> BoxFuture<'static, Result<Response>> {
184+
pub async fn send(&self, req: impl Into<Request>) -> Result<Response> {
186185
let req: Request = req.into();
187186
let http_client = self.http_client.clone();
188187
let middleware = self.middleware.clone();
189-
Box::pin(async move {
190-
let next = Next::new(&middleware, &|req, client| {
191-
Box::pin(async move {
192-
let req: http_types::Request = req.into();
193-
client.http_client.send(req).await.map(Into::into)
194-
})
195-
});
196-
197-
let res = next
198-
.run(req, Self::with_http_client_internal(http_client))
199-
.await?;
200-
Ok(Response::new(res.into()))
201-
})
188+
189+
let next = Next::new(&middleware, &|req, client| {
190+
Box::pin(async move {
191+
let req: http_types::Request = req.into();
192+
client.http_client.send(req).await.map(Into::into)
193+
})
194+
});
195+
196+
let res = next
197+
.run(req, Self::with_http_client_internal(http_client))
198+
.await?;
199+
Ok(Response::new(res.into()))
202200
}
203201

204202
/// Submit a `Request` and get the response body as bytes.

src/request_builder.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ impl RequestBuilder {
223223
}
224224

225225
/// Create a `Client` and send the constructed `Request` from it.
226-
pub fn send(mut self) -> BoxFuture<'static, Result<Response>> {
226+
pub async fn send(mut self) -> Result<Response> {
227227
self.client
228228
.take()
229229
.unwrap_or_else(Client::new_shared_or_panic)
230230
.send(self.build())
231+
.await
231232
}
232233
}
233234

@@ -243,12 +244,13 @@ impl Future for RequestBuilder {
243244
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
244245
if self.fut.is_none() {
245246
let req = self.req.take().unwrap();
246-
if let Some(client) = &self.client {
247-
self.fut = Some(client.send(req))
248-
} else {
249-
let client = Client::new_shared_or_panic();
250-
self.fut = Some(client.send(req))
251-
}
247+
248+
let client = self
249+
.client
250+
.take()
251+
.unwrap_or_else(Client::new_shared_or_panic);
252+
253+
self.fut = Some(Box::pin(async move { client.send(req).await }))
252254
}
253255

254256
// We can safely unwrap here because this is the only time we take ownership of the request.

0 commit comments

Comments
 (0)