Skip to content

Commit 10178c5

Browse files
authored
Merge pull request #247 from Fishrock123/client-send-no-boxedfuture
Client, RB: remove BoxFuture from .send()
2 parents c66f051 + 9e438c0 commit 10178c5

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
@@ -258,11 +258,12 @@ impl RequestBuilder {
258258
}
259259

260260
/// Create a `Client` and send the constructed `Request` from it.
261-
pub fn send(mut self) -> BoxFuture<'static, Result<Response>> {
261+
pub async fn send(mut self) -> Result<Response> {
262262
self.client
263263
.take()
264264
.unwrap_or_else(Client::new_shared_or_panic)
265265
.send(self.build())
266+
.await
266267
}
267268
}
268269

@@ -278,12 +279,13 @@ impl Future for RequestBuilder {
278279
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
279280
if self.fut.is_none() {
280281
let req = self.req.take().unwrap();
281-
if let Some(client) = &self.client {
282-
self.fut = Some(client.send(req))
283-
} else {
284-
let client = Client::new_shared_or_panic();
285-
self.fut = Some(client.send(req))
286-
}
282+
283+
let client = self
284+
.client
285+
.take()
286+
.unwrap_or_else(Client::new_shared_or_panic);
287+
288+
self.fut = Some(Box::pin(async move { client.send(req).await }))
287289
}
288290

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

0 commit comments

Comments
 (0)