Skip to content

Commit

Permalink
Reapply "feat: Add impl Service<http::Request<Body>> for Client a…
Browse files Browse the repository at this point in the history
…nd `&'_ Client` (seanmonstar#2356)"

This reverts commit 68127f0.
  • Loading branch information
catbrained committed Dec 31, 2024
1 parent 224f0b8 commit 09da238
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,62 @@ impl tower_service::Service<Request> for &'_ Client {
}
}

impl tower_service::Service<http::Request<crate::Body>> for Client {
type Response = http::Response<crate::Body>;
type Error = crate::Error;
type Future = MappedPending;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
match req.try_into() {
Ok(req) => MappedPending::new(self.execute_request(req)),
Err(err) => MappedPending::new(Pending::new_err(err)),
}
}
}

impl tower_service::Service<http::Request<crate::Body>> for &'_ Client {
type Response = http::Response<crate::Body>;
type Error = crate::Error;
type Future = MappedPending;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
match req.try_into() {
Ok(req) => MappedPending::new(self.execute_request(req)),
Err(err) => MappedPending::new(Pending::new_err(err)),
}
}
}

pin_project! {
pub struct MappedPending {
#[pin]
inner: Pending,
}
}

impl MappedPending {
fn new(inner: Pending) -> MappedPending {
Self { inner }
}
}

impl Future for MappedPending {
type Output = Result<http::Response<crate::Body>, crate::Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = self.project().inner;
inner.poll(cx).map_ok(Into::into)
}
}

impl fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut builder = f.debug_struct("ClientBuilder");
Expand Down

0 comments on commit 09da238

Please sign in to comment.