diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml index 50d8aa1fe..24fb0b9d6 100644 --- a/tonic/Cargo.toml +++ b/tonic/Cargo.toml @@ -31,7 +31,7 @@ prost = ["dep:prost"] tls = ["dep:rustls-pemfile", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] tls-roots = ["tls", "channel", "dep:rustls-native-certs"] tls-webpki-roots = ["tls", "channel", "dep:webpki-roots"] -router = ["dep:axum"] +router = ["dep:axum", "dep:tower", "tower?/util"] server = [ "router", "dep:async-stream", diff --git a/tonic/src/service/router.rs b/tonic/src/service/router.rs index e7f864f8d..61b69d4df 100644 --- a/tonic/src/service/router.rs +++ b/tonic/src/service/router.rs @@ -12,7 +12,7 @@ use std::{ pin::Pin, task::{ready, Context, Poll}, }; -use tower_service::Service; +use tower::{Service, ServiceExt}; /// A [`Service`] router. #[derive(Debug, Default, Clone)] @@ -77,7 +77,7 @@ impl Routes { { self.router = self.router.route_service( &format!("/{}/*rest", S::NAME), - AxumBodyService { service: svc }, + svc.map_request(|req: Request| req.map(boxed)), ); self } @@ -143,32 +143,3 @@ impl Future for RoutesFuture { } } } - -#[derive(Clone)] -struct AxumBodyService { - service: S, -} - -type BoxFuture<'a, T> = Pin + Send + 'a>>; - -impl Service> for AxumBodyService -where - S: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, - S::Future: Send + 'static, -{ - type Response = Response; - type Error = Infallible; - type Future = BoxFuture<'static, Result>; - - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.service.poll_ready(cx) - } - - fn call(&mut self, req: Request) -> Self::Future { - let fut = self.service.call(req.map(boxed)); - Box::pin(async move { fut.await.map(|res| res.map(axum::body::Body::new)) }) - } -}