Add unique future types for all services (#157)

So we can more easily change them in the future.
This commit is contained in:
David Pedersen
2021-08-07 22:27:27 +02:00
committed by GitHub
parent 2389761ce7
commit 6ce355cca3
7 changed files with 106 additions and 14 deletions
+34 -2
View File
@@ -1,11 +1,43 @@
//! Handler future types.
use crate::body::BoxBody;
use http::Response;
use std::convert::Infallible;
use http::{Request, Response};
use pin_project_lite::pin_project;
use std::{
convert::Infallible,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::Service;
opaque_future! {
/// The response future for [`IntoService`](super::IntoService).
pub type IntoServiceFuture =
futures_util::future::BoxFuture<'static, Result<Response<BoxBody>, Infallible>>;
}
pin_project! {
/// The response future for [`OnMethod`](super::OnMethod).
#[derive(Debug)]
pub struct OnMethodFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>
{
#[pin]
pub(super) inner: crate::routing::future::RouteFuture<S, F, B>,
}
}
impl<S, F, B> Future for OnMethodFuture<S, F, B>
where
S: Service<Request<B>, Response = Response<BoxBody>>,
F: Service<Request<B>, Response = Response<BoxBody>, Error = S::Error>,
{
type Output = Result<Response<BoxBody>, S::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx)
}
}
+5 -3
View File
@@ -617,19 +617,21 @@ where
{
type Response = Response<BoxBody>;
type Error = Infallible;
type Future = RouteFuture<S, F, B>;
type Future = future::OnMethodFuture<S, F, B>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<B>) -> Self::Future {
if self.method.matches(req.method()) {
let f = if self.method.matches(req.method()) {
let fut = self.svc.clone().oneshot(req);
RouteFuture::a(fut)
} else {
let fut = self.fallback.clone().oneshot(req);
RouteFuture::b(fut)
}
};
future::OnMethodFuture { inner: f }
}
}