Skip to content

Commit 6ecac07

Browse files
committed
feat: make HttpClient dyn-clonable
Kinda reverts http-rs#48 Related to http-rs/surf#237 Desirable for Surf-level config.
1 parent cdb4972 commit 6ecac07

File tree

6 files changed

+14
-6
lines changed

6 files changed

+14
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ hyper = { version = "0.13.6", features = ["tcp"], optional = true }
5757
hyper-tls = { version = "0.4.3", optional = true }
5858
futures-util = { version = "0.3.5", features = ["io"], optional = true }
5959
tokio = { version = "0.2", features = ["time"], optional = true }
60+
dyn-clone = "1.0.4"
61+
dyn-clonable = "0.9.0"
6062

6163
# curl_client
6264
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

src/h1/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type HttpPool = DashMap<SocketAddr, Pool<TcpStream, std::io::Error>>;
4040
#[cfg(any(feature = "native-tls", feature = "rustls"))]
4141
type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
4242

43-
/// Async-h1 based HTTP Client, with connecton pooling ("Keep-Alive").
43+
/// Async-h1 based HTTP Client, with connection pooling ("Keep-Alive").
44+
#[derive(Clone)]
4445
pub struct H1Client {
4546
http_pools: HttpPool,
4647
#[cfg(any(feature = "native-tls", feature = "rustls"))]

src/hyper.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::Debug;
77
use std::io;
88
use std::str::FromStr;
99

10+
use dyn_clonable::*;
1011
use futures_util::stream::TryStreamExt;
1112
use http_types::headers::{HeaderName, HeaderValue};
1213
use http_types::StatusCode;
@@ -21,7 +22,8 @@ use super::{async_trait, Error, HttpClient, Request, Response};
2122
type HyperRequest = hyper::Request<hyper::Body>;
2223

2324
// Avoid leaking Hyper generics into HttpClient by hiding it behind a dynamic trait object pointer.
24-
trait HyperClientObject: Debug + Send + Sync + 'static {
25+
#[clonable]
26+
trait HyperClientObject: Clone + Debug + Send + Sync + 'static {
2527
fn dyn_request(&self, req: hyper::Request<hyper::Body>) -> hyper::client::ResponseFuture;
2628
}
2729

@@ -32,7 +34,7 @@ impl<C: Clone + Connect + Debug + Send + Sync + 'static> HyperClientObject for h
3234
}
3335

3436
/// Hyper-based HTTP Client.
35-
#[derive(Debug)]
37+
#[derive(Clone, Debug)]
3638
pub struct HyperClient {
3739
client: Box<dyn HyperClientObject>,
3840
config: Config,

src/isahc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::Config;
1313
use super::{async_trait, Body, Error, HttpClient, Request, Response};
1414

1515
/// Curl-based HTTP Client.
16-
#[derive(Debug)]
16+
#[derive(Clone, Debug)]
1717
pub struct IsahcClient {
1818
client: isahc::HttpClient,
1919
config: Config,

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub type Request = http_types::Request;
4848
/// An HTTP Response type with a streaming body.
4949
pub type Response = http_types::Response;
5050

51+
use dyn_clonable::*;
52+
5153
pub use async_trait::async_trait;
5254
pub use http_types;
5355

@@ -64,7 +66,8 @@ pub use http_types;
6466
/// though middleware for one of its own requests, and in order to do so should be wrapped in an
6567
/// `Rc`/`Arc` to enable reference cloning.
6668
#[async_trait]
67-
pub trait HttpClient: std::fmt::Debug + Unpin + Send + Sync + 'static {
69+
#[clonable]
70+
pub trait HttpClient: Clone + std::fmt::Debug + Unpin + Send + Sync + 'static {
6871
/// Perform a request.
6972
async fn send(&self, req: Request) -> Result<Response, Error>;
7073

src/wasm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::pin::Pin;
99
use std::task::{Context, Poll};
1010

1111
/// WebAssembly HTTP Client.
12-
#[derive(Debug)]
12+
#[derive(Clone, Debug)]
1313
pub struct WasmClient {
1414
_priv: (),
1515
}

0 commit comments

Comments
 (0)