diff --git a/tonic/src/transport/server/service/recover_error.rs b/tonic/src/transport/server/service/recover_error.rs index 7b89919b4..ec9dfdc8b 100644 --- a/tonic/src/transport/server/service/recover_error.rs +++ b/tonic/src/transport/server/service/recover_error.rs @@ -1,13 +1,14 @@ -use crate::Status; -use http::Response; -use http_body::Frame; -use pin_project::pin_project; use std::{ future::Future, pin::Pin, task::{ready, Context, Poll}, }; -use tower::Service; + +use http::Response; +use pin_project::pin_project; +use tower_service::Service; + +use crate::Status; /// Middleware that attempts to recover from service errors by turning them into a response built /// from the `Status`. @@ -22,12 +23,12 @@ impl RecoverError { } } -impl Service for RecoverError +impl Service for RecoverError where - S: Service>, + S: Service>, S::Error: Into, { - type Response = Response>; + type Response = Response>; type Error = crate::BoxError; type Future = ResponseFuture; @@ -35,7 +36,7 @@ where self.inner.poll_ready(cx).map_err(Into::into) } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { ResponseFuture { inner: self.inner.call(req), } @@ -53,21 +54,18 @@ where F: Future, E>>, E: Into, { - type Output = Result>, crate::BoxError>; + type Output = Result>, crate::BoxError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let result: Result, crate::BoxError> = - ready!(self.project().inner.poll(cx)).map_err(Into::into); - - match result { + match ready!(self.project().inner.poll(cx)) { Ok(response) => { - let response = response.map(MaybeEmptyBody::full); + let response = response.map(ResponseBody::full); Poll::Ready(Ok(response)) } - Err(err) => match Status::try_from_error(err) { + Err(err) => match Status::try_from_error(err.into()) { Ok(status) => { let (parts, ()) = status.into_http::<()>().into_parts(); - let res = Response::from_parts(parts, MaybeEmptyBody::empty()); + let res = Response::from_parts(parts, ResponseBody::empty()); Poll::Ready(Ok(res)) } Err(err) => Poll::Ready(Err(err)), @@ -77,22 +75,22 @@ where } #[pin_project] -pub(crate) struct MaybeEmptyBody { +pub(crate) struct ResponseBody { #[pin] inner: Option, } -impl MaybeEmptyBody { +impl ResponseBody { fn full(inner: B) -> Self { Self { inner: Some(inner) } } - fn empty() -> Self { + const fn empty() -> Self { Self { inner: None } } } -impl http_body::Body for MaybeEmptyBody +impl http_body::Body for ResponseBody where B: http_body::Body, { @@ -102,7 +100,7 @@ where fn poll_frame( self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll, Self::Error>>> { + ) -> Poll, Self::Error>>> { match self.project().inner.as_pin_mut() { Some(b) => b.poll_frame(cx), None => Poll::Ready(None),