From 66b2191489da8dc4e5714b0de5377961aae40c5c Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Thu, 24 Sep 2020 10:51:08 -0700 Subject: [PATCH] client: make with_http_client() generic This avoids leaking out the storage underneath. Internally we require a function like the original though, due to very confusing & indirect lifetime requirements. --- src/client.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1a053cb..934b279 100644 --- a/src/client.rs +++ b/src/client.rs @@ -80,7 +80,7 @@ impl Client { /// ``` #[cfg(feature = "default-client")] pub fn new() -> Self { - Self::with_http_client(Arc::new(DefaultClient::new())) + Self::with_http_client(DefaultClient::new()) } pub(crate) fn new_shared_or_panic() -> Self { @@ -101,11 +101,14 @@ impl Client { /// # #[cfg(feature = "curl-client")] { /// # use std::sync::Arc; /// use http_client::isahc::IsahcClient; - /// let client = surf::Client::with_http_client(Arc::new(IsahcClient::new())); + /// let client = surf::Client::with_http_client(IsahcClient::new()); /// # } /// ``` + pub fn with_http_client(http_client: C) -> Self { + Self::with_http_client_internal(Arc::new(http_client)) + } - pub fn with_http_client(http_client: Arc) -> Self { + fn with_http_client_internal(http_client: Arc) -> Self { let client = Self { base_url: None, http_client, @@ -122,7 +125,7 @@ impl Client { pub(crate) fn new_shared() -> Self { cfg_if! { if #[cfg(feature = "curl-client")] { - Self::with_http_client(Arc::new(GLOBAL_CLIENT.clone())) + Self::with_http_client(GLOBAL_CLIENT.clone()) } else { Self::new() } @@ -177,7 +180,9 @@ impl Client { }) }); - let res = next.run(req, Client::with_http_client(http_client)).await?; + let res = next + .run(req, Self::with_http_client_internal(http_client)) + .await?; Ok(Response::new(res.into())) }) }