Skip to content

Commit 522216e

Browse files
feat: Add impl Service<http::Request<Body>> for Client and &'_ Client (#2356)
1 parent 646b1f8 commit 522216e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
- Implement `danger_accept_invalid_hostnames` for `rustls`.
4+
- Add `impl Service<http::Request<Body>>` for `Client` and `&'_ Client`.
45

56
## v0.12.5
67

src/async_impl/client.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,62 @@ impl tower_service::Service<Request> for &'_ Client {
21612161
}
21622162
}
21632163

2164+
impl tower_service::Service<http::Request<crate::Body>> for Client {
2165+
type Response = http::Response<crate::Body>;
2166+
type Error = crate::Error;
2167+
type Future = MappedPending;
2168+
2169+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2170+
Poll::Ready(Ok(()))
2171+
}
2172+
2173+
fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
2174+
match req.try_into() {
2175+
Ok(req) => MappedPending::new(self.execute_request(req)),
2176+
Err(err) => MappedPending::new(Pending::new_err(err)),
2177+
}
2178+
}
2179+
}
2180+
2181+
impl tower_service::Service<http::Request<crate::Body>> for &'_ Client {
2182+
type Response = http::Response<crate::Body>;
2183+
type Error = crate::Error;
2184+
type Future = MappedPending;
2185+
2186+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2187+
Poll::Ready(Ok(()))
2188+
}
2189+
2190+
fn call(&mut self, req: http::Request<crate::Body>) -> Self::Future {
2191+
match req.try_into() {
2192+
Ok(req) => MappedPending::new(self.execute_request(req)),
2193+
Err(err) => MappedPending::new(Pending::new_err(err)),
2194+
}
2195+
}
2196+
}
2197+
2198+
pin_project! {
2199+
pub struct MappedPending {
2200+
#[pin]
2201+
inner: Pending,
2202+
}
2203+
}
2204+
2205+
impl MappedPending {
2206+
fn new(inner: Pending) -> MappedPending {
2207+
Self { inner }
2208+
}
2209+
}
2210+
2211+
impl Future for MappedPending {
2212+
type Output = Result<http::Response<crate::Body>, crate::Error>;
2213+
2214+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2215+
let inner = self.project().inner;
2216+
inner.poll(cx).map_ok(Into::into)
2217+
}
2218+
}
2219+
21642220
impl fmt::Debug for ClientBuilder {
21652221
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21662222
let mut builder = f.debug_struct("ClientBuilder");

0 commit comments

Comments
 (0)