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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 14 deletions

View File

@ -49,6 +49,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `UrlParamsRejection`
- `InvalidUrlParam`
- Removed `extract::UrlParams` and `extract::UrlParamsMap`. Use `extract::Path` instead
- The following services have new response future types:
- `service::OnMethod`
- `handler::OnMethod`
- `routing::Nested`
# 0.1.3 (06. August, 2021)

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)
}
}

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 }
}
}

View File

@ -1,6 +1,6 @@
//! Routing between [`Service`]s.
use self::future::{BoxRouteFuture, EmptyRouterFuture, RouteFuture};
use self::future::{BoxRouteFuture, EmptyRouterFuture, NestedFuture, RouteFuture};
use crate::{
body::{box_body, BoxBody},
buffer::MpscBuffer,
@ -898,7 +898,7 @@ where
{
type Response = Response<BoxBody>;
type Error = S::Error;
type Future = RouteFuture<S, F, B>;
type Future = NestedFuture<S, F, B>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -910,7 +910,7 @@ where
req.extensions_mut().insert(original_uri);
}
if let Some((prefix, captures)) = self.pattern.prefix_match(req.uri().path()) {
let f = if let Some((prefix, captures)) = self.pattern.prefix_match(req.uri().path()) {
let without_prefix = strip_prefix(req.uri(), prefix);
*req.uri_mut() = without_prefix;
@ -920,7 +920,9 @@ where
} else {
let fut = self.fallback.clone().oneshot(req);
RouteFuture::b(fut)
}
};
NestedFuture { inner: f }
}
}

View File

@ -115,3 +115,28 @@ where
}
}
}
pin_project! {
/// The response future for [`Nested`](super::Nested).
#[derive(Debug)]
pub struct NestedFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>
{
#[pin]
pub(super) inner: RouteFuture<S, F, B>,
}
}
impl<S, F, B> Future for NestedFuture<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)
}
}

View File

@ -6,14 +6,14 @@ use crate::{
};
use bytes::Bytes;
use futures_util::ready;
use http::Response;
use http::{Request, Response};
use pin_project_lite::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::BoxError;
use tower::{BoxError, Service};
pin_project! {
/// Response future for [`HandleError`](super::HandleError).
@ -74,3 +74,28 @@ where
Poll::Ready(Ok(res))
}
}
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)
}
}

View File

@ -439,20 +439,22 @@ where
{
type Response = Response<BoxBody>;
type Error = S::Error;
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 }
}
}