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.
This commit is contained in:
David Pedersen 2021-10-26 01:23:52 +02:00
parent e949c47df6
commit efdd197643
8 changed files with 42 additions and 9 deletions

View file

@ -27,10 +27,12 @@ impl<T, U, E> Service<T> for CloneBoxService<T, U, E> {
type Error = E;
type Future = BoxFuture<'static, Result<U, E>>;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
self.0.poll_ready(cx)
}
#[inline]
fn call(&mut self, request: T) -> Self::Future {
self.0.call(request)
}

View file

@ -132,6 +132,7 @@ where
type Error = Infallible;
type Future = HandleErrorFuture<Oneshot<S, Request<ReqBody>>, F>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

View file

@ -62,6 +62,7 @@ where
type Error = Infallible;
type Future = super::future::IntoServiceFuture;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
// `IntoService` can only be constructed from async functions which are always ready, or from
// `Layered` which bufferes in `<Layered as Handler>::call` and is therefore also always

View file

@ -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<Self::Output> {
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.project().future.poll(cx)
}
}

View file

@ -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<B> RouterFuture<B> {
}
}
opaque_future! {
pin_project! {
/// Response future for [`Route`](super::Route).
pub type RouteFuture =
futures_util::future::BoxFuture<'static, Result<Response<BoxBody>, Infallible>>;
pub struct RouteFuture<B> {
#[pin]
future: Oneshot<
CloneBoxService<Request<B>, Response<BoxBody>, Infallible>,
Request<B>,
>
}
}
impl<B> RouteFuture<B> {
pub(crate) fn new(
future: Oneshot<CloneBoxService<Request<B>, Response<BoxBody>, Infallible>, Request<B>>,
) -> Self {
RouteFuture { future }
}
}
impl<B> Future for RouteFuture<B> {
type Output = Result<Response<BoxBody>, Infallible>;
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().future.poll(cx)
}
}
opaque_future! {

View file

@ -416,6 +416,7 @@ where
type Error = Infallible;
type Future = MethodRouterFuture<F, B>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

View file

@ -1012,6 +1012,7 @@ where
type Error = Infallible;
type Future = future::MakeRouteServiceFuture<S>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
@ -1041,16 +1042,16 @@ impl<ReqBody> fmt::Debug for Route<ReqBody> {
impl<B> Service<Request<B>> for Route<B> {
type Response = Response<BoxBody>;
type Error = Infallible;
type Future = RouteFuture;
type Future = RouteFuture<B>;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.0.poll_ready(cx)
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
#[inline]
fn call(&mut self, req: Request<B>) -> Self::Future {
RouteFuture::new(self.0.call(req))
RouteFuture::new(self.0.clone().oneshot(req))
}
}

View file

@ -492,6 +492,7 @@ where
type Error = S::Error;
type Future = MethodRouterFuture<S, F, B>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}