From efdd197643c0c46783378fd7e575f48a494e7fcb Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 26 Oct 2021 01:23:52 +0200 Subject: [PATCH] More consistent `poll_ready` usage The `#[inline]`s probably aren't necessary but are consistent with other parts of the code and tower in general. --- src/clone_box_service.rs | 2 ++ src/error_handling/mod.rs | 1 + src/handler/into_service.rs | 1 + src/macros.rs | 6 +++++- src/routing/future.rs | 30 ++++++++++++++++++++++++---- src/routing/handler_method_router.rs | 1 + src/routing/mod.rs | 9 +++++---- src/routing/service_method_router.rs | 1 + 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/clone_box_service.rs b/src/clone_box_service.rs index b659feec..e75c71dd 100644 --- a/src/clone_box_service.rs +++ b/src/clone_box_service.rs @@ -27,10 +27,12 @@ impl Service for CloneBoxService { type Error = E; type Future = BoxFuture<'static, Result>; + #[inline] fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.0.poll_ready(cx) } + #[inline] fn call(&mut self, request: T) -> Self::Future { self.0.call(request) } diff --git a/src/error_handling/mod.rs b/src/error_handling/mod.rs index a925bdb6..5264c200 100644 --- a/src/error_handling/mod.rs +++ b/src/error_handling/mod.rs @@ -132,6 +132,7 @@ where type Error = Infallible; type Future = HandleErrorFuture>, F>; + #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } diff --git a/src/handler/into_service.rs b/src/handler/into_service.rs index efc27aff..ffde1359 100644 --- a/src/handler/into_service.rs +++ b/src/handler/into_service.rs @@ -62,6 +62,7 @@ where type Error = Infallible; type Future = super::future::IntoServiceFuture; + #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { // `IntoService` can only be constructed from async functions which are always ready, or from // `Layered` which bufferes in `::call` and is therefore also always diff --git a/src/macros.rs b/src/macros.rs index a705e76c..bdb93405 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -33,8 +33,12 @@ macro_rules! opaque_future { $actual: std::future::Future, { type Output = <$actual as std::future::Future>::Output; + #[inline] - fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll { + fn poll( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll { self.project().future.poll(cx) } } diff --git a/src/routing/future.rs b/src/routing/future.rs index 7a71d79b..43cbebd7 100644 --- a/src/routing/future.rs +++ b/src/routing/future.rs @@ -1,6 +1,6 @@ //! Future types. -use crate::body::BoxBody; +use crate::{body::BoxBody, clone_box_service::CloneBoxService}; use futures_util::future::Either; use http::{Request, Response}; use pin_project_lite::pin_project; @@ -32,10 +32,32 @@ impl RouterFuture { } } -opaque_future! { +pin_project! { /// Response future for [`Route`](super::Route). - pub type RouteFuture = - futures_util::future::BoxFuture<'static, Result, Infallible>>; + pub struct RouteFuture { + #[pin] + future: Oneshot< + CloneBoxService, Response, Infallible>, + Request, + > + } +} + +impl RouteFuture { + pub(crate) fn new( + future: Oneshot, Response, Infallible>, Request>, + ) -> Self { + RouteFuture { future } + } +} + +impl Future for RouteFuture { + type Output = Result, Infallible>; + + #[inline] + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + self.project().future.poll(cx) + } } opaque_future! { diff --git a/src/routing/handler_method_router.rs b/src/routing/handler_method_router.rs index 59fc8898..74cab644 100644 --- a/src/routing/handler_method_router.rs +++ b/src/routing/handler_method_router.rs @@ -416,6 +416,7 @@ where type Error = Infallible; type Future = MethodRouterFuture; + #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } diff --git a/src/routing/mod.rs b/src/routing/mod.rs index f4f480d1..7f027f36 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -1012,6 +1012,7 @@ where type Error = Infallible; type Future = future::MakeRouteServiceFuture; + #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } @@ -1041,16 +1042,16 @@ impl fmt::Debug for Route { impl Service> for Route { type Response = Response; type Error = Infallible; - type Future = RouteFuture; + type Future = RouteFuture; #[inline] - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.0.poll_ready(cx) + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) } #[inline] fn call(&mut self, req: Request) -> Self::Future { - RouteFuture::new(self.0.call(req)) + RouteFuture::new(self.0.clone().oneshot(req)) } } diff --git a/src/routing/service_method_router.rs b/src/routing/service_method_router.rs index 36c0f5c7..ab5f7579 100644 --- a/src/routing/service_method_router.rs +++ b/src/routing/service_method_router.rs @@ -492,6 +492,7 @@ where type Error = S::Error; type Future = MethodRouterFuture; + #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) }