Skip to content

Commit 1ba0678

Browse files
committed
HttpClient: remove Error type bounds
This makes using HttpClient as a dynamic Trait object, such as in http-rs/surf#69 substantially more complex. This complexity can be reduced by having the backing client implementor handle the error translation (which was already required to begin with).
1 parent 44a32b4 commit 1ba0678

File tree

4 files changed

+9
-18
lines changed

4 files changed

+9
-18
lines changed

src/h1.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! http-client implementation for async-h1.
22
3-
use super::{HttpClient, Request, Response};
3+
use super::{Error, HttpClient, Request, Response};
44

55
use async_h1::client;
66
use futures::future::BoxFuture;
7-
use http_types::{Error, StatusCode};
7+
use http_types::StatusCode;
88

99
/// Async-h1 based HTTP Client.
1010
#[derive(Debug)]
@@ -30,9 +30,7 @@ impl Clone for H1Client {
3030
}
3131

3232
impl HttpClient for H1Client {
33-
type Error = Error;
34-
35-
fn send(&self, mut req: Request) -> BoxFuture<'static, Result<Response, Self::Error>> {
33+
fn send(&self, mut req: Request) -> BoxFuture<'static, Result<Response, Error>> {
3634
Box::pin(async move {
3735
// Insert host
3836
let host = req

src/isahc.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! http-client implementation for isahc
22
3-
use super::{Body, HttpClient, Request, Response};
3+
use super::{Body, Error, HttpClient, Request, Response};
44

55
use async_std::io::BufReader;
66
use futures::future::BoxFuture;
@@ -42,9 +42,7 @@ impl Clone for IsahcClient {
4242
}
4343

4444
impl HttpClient for IsahcClient {
45-
type Error = isahc::Error;
46-
47-
fn send(&self, mut req: Request) -> BoxFuture<'static, Result<Response, Self::Error>> {
45+
fn send(&self, mut req: Request) -> BoxFuture<'static, Result<Response, Error>> {
4846
let client = self.client.clone();
4947
Box::pin(async move {
5048
let mut builder = http::Request::builder()
@@ -65,7 +63,7 @@ impl HttpClient for IsahcClient {
6563
};
6664

6765
let request = builder.body(body).unwrap();
68-
let res = client.send_async(request).await?;
66+
let res = client.send_async(request).await.map_err(Error::from)?;
6967
let (parts, body) = res.into_parts();
7068
let len = body.len().map(|len| len as usize);
7169
let body = Body::from_reader(BufReader::new(body), len);

src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ pub use http_types;
5353
/// though middleware for one of its own requests, and in order to do so should be wrapped in an
5454
/// `Rc`/`Arc` to enable reference cloning.
5555
pub trait HttpClient: std::fmt::Debug + Unpin + Send + Sync + 'static {
56-
/// The associated error type.
57-
type Error: Send + Sync + Into<Error>;
58-
5956
/// Perform a request.
60-
fn send(&self, req: Request) -> BoxFuture<'static, Result<Response, Self::Error>>;
57+
fn send(&self, req: Request) -> BoxFuture<'static, Result<Response, Error>>;
6158
}
6259

6360
/// The raw body of an http request or response.

src/wasm.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! http-client implementation for fetch
22
3-
use super::{Body, HttpClient, Request, Response};
3+
use super::{Body, Error, HttpClient, Request, Response};
44

55
use futures::future::BoxFuture;
66
use futures::prelude::*;
@@ -29,9 +29,7 @@ impl Clone for WasmClient {
2929
}
3030

3131
impl HttpClient for WasmClient {
32-
type Error = std::io::Error;
33-
34-
fn send(&self, req: Request) -> BoxFuture<'static, Result<Response, Self::Error>> {
32+
fn send(&self, req: Request) -> BoxFuture<'static, Result<Response, Error>> {
3533
let fut = Box::pin(async move {
3634
let url = format!("{}", req.uri());
3735
let req = fetch::new(req.method().as_str(), &url);

0 commit comments

Comments
 (0)