mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-28 21:40:55 +00:00
Add unique future types for all services (#157)
So we can more easily change them in the future.
This commit is contained in:
parent
2389761ce7
commit
6ce355cca3
@ -49,6 +49,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- `UrlParamsRejection`
|
- `UrlParamsRejection`
|
||||||
- `InvalidUrlParam`
|
- `InvalidUrlParam`
|
||||||
- Removed `extract::UrlParams` and `extract::UrlParamsMap`. Use `extract::Path` instead
|
- 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)
|
# 0.1.3 (06. August, 2021)
|
||||||
|
|
||||||
|
@ -1,11 +1,43 @@
|
|||||||
//! Handler future types.
|
//! Handler future types.
|
||||||
|
|
||||||
use crate::body::BoxBody;
|
use crate::body::BoxBody;
|
||||||
use http::Response;
|
use http::{Request, Response};
|
||||||
use std::convert::Infallible;
|
use pin_project_lite::pin_project;
|
||||||
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
|
future::Future,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use tower::Service;
|
||||||
|
|
||||||
opaque_future! {
|
opaque_future! {
|
||||||
/// The response future for [`IntoService`](super::IntoService).
|
/// The response future for [`IntoService`](super::IntoService).
|
||||||
pub type IntoServiceFuture =
|
pub type IntoServiceFuture =
|
||||||
futures_util::future::BoxFuture<'static, Result<Response<BoxBody>, Infallible>>;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -617,19 +617,21 @@ where
|
|||||||
{
|
{
|
||||||
type Response = Response<BoxBody>;
|
type Response = Response<BoxBody>;
|
||||||
type Error = Infallible;
|
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>> {
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request<B>) -> Self::Future {
|
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);
|
let fut = self.svc.clone().oneshot(req);
|
||||||
RouteFuture::a(fut)
|
RouteFuture::a(fut)
|
||||||
} else {
|
} else {
|
||||||
let fut = self.fallback.clone().oneshot(req);
|
let fut = self.fallback.clone().oneshot(req);
|
||||||
RouteFuture::b(fut)
|
RouteFuture::b(fut)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
future::OnMethodFuture { inner: f }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Routing between [`Service`]s.
|
//! Routing between [`Service`]s.
|
||||||
|
|
||||||
use self::future::{BoxRouteFuture, EmptyRouterFuture, RouteFuture};
|
use self::future::{BoxRouteFuture, EmptyRouterFuture, NestedFuture, RouteFuture};
|
||||||
use crate::{
|
use crate::{
|
||||||
body::{box_body, BoxBody},
|
body::{box_body, BoxBody},
|
||||||
buffer::MpscBuffer,
|
buffer::MpscBuffer,
|
||||||
@ -898,7 +898,7 @@ where
|
|||||||
{
|
{
|
||||||
type Response = Response<BoxBody>;
|
type Response = Response<BoxBody>;
|
||||||
type Error = S::Error;
|
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>> {
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
@ -910,7 +910,7 @@ where
|
|||||||
req.extensions_mut().insert(original_uri);
|
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);
|
let without_prefix = strip_prefix(req.uri(), prefix);
|
||||||
*req.uri_mut() = without_prefix;
|
*req.uri_mut() = without_prefix;
|
||||||
|
|
||||||
@ -920,7 +920,9 @@ where
|
|||||||
} else {
|
} else {
|
||||||
let fut = self.fallback.clone().oneshot(req);
|
let fut = self.fallback.clone().oneshot(req);
|
||||||
RouteFuture::b(fut)
|
RouteFuture::b(fut)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
NestedFuture { inner: f }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,14 +6,14 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_util::ready;
|
use futures_util::ready;
|
||||||
use http::Response;
|
use http::{Request, Response};
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
future::Future,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
use tower::BoxError;
|
use tower::{BoxError, Service};
|
||||||
|
|
||||||
pin_project! {
|
pin_project! {
|
||||||
/// Response future for [`HandleError`](super::HandleError).
|
/// Response future for [`HandleError`](super::HandleError).
|
||||||
@ -74,3 +74,28 @@ where
|
|||||||
Poll::Ready(Ok(res))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -439,20 +439,22 @@ where
|
|||||||
{
|
{
|
||||||
type Response = Response<BoxBody>;
|
type Response = Response<BoxBody>;
|
||||||
type Error = S::Error;
|
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>> {
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request<B>) -> Self::Future {
|
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);
|
let fut = self.svc.clone().oneshot(req);
|
||||||
RouteFuture::a(fut)
|
RouteFuture::a(fut)
|
||||||
} else {
|
} else {
|
||||||
let fut = self.fallback.clone().oneshot(req);
|
let fut = self.fallback.clone().oneshot(req);
|
||||||
RouteFuture::b(fut)
|
RouteFuture::b(fut)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
future::OnMethodFuture { inner: f }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user