Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move response futures into their own modules #130

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/extract/extractor_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ where
{
type Response = Response<BoxBody>;
type Error = S::Error;
type Future = ExtractorMiddlewareResponseFuture<ReqBody, S, E>;
type Future = ResponseFuture<ReqBody, S, E>;

#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Expand All @@ -177,7 +177,7 @@ where
(req, extracted)
});

ExtractorMiddlewareResponseFuture {
ResponseFuture {
state: State::Extracting {
future: extract_future,
},
Expand All @@ -186,10 +186,17 @@ where
}
}

#[doc(hidden)]
#[deprecated(
since = "0.1.3",
note = "Use axum::extract::extractor_middleware::ResponseFuture"
)]
pub type ExtractorMiddlewareResponseFuture<B, S, E> = ResponseFuture<B, S, E>;

pin_project! {
/// Response future for [`ExtractorMiddleware`].
#[allow(missing_debug_implementations)]
pub struct ExtractorMiddlewareResponseFuture<ReqBody, S, E>
pub struct ResponseFuture<ReqBody, S, E>
where
E: FromRequest<ReqBody>,
S: Service<Request<ReqBody>>,
Expand All @@ -212,7 +219,7 @@ pin_project! {
}
}

impl<ReqBody, S, E, ResBody> Future for ExtractorMiddlewareResponseFuture<ReqBody, S, E>
impl<ReqBody, S, E, ResBody> Future for ResponseFuture<ReqBody, S, E>
where
E: FromRequest<ReqBody>,
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
Expand Down
108 changes: 9 additions & 99 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@ use crate::{
};
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::future;
use http::{Method, Request, Response, StatusCode, Uri};
use pin_project_lite::pin_project;
use regex::Regex;
use std::{
borrow::Cow,
convert::Infallible,
fmt,
future::Future,
marker::PhantomData,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tower::{
util::{BoxService, Oneshot, ServiceExt},
util::{BoxService, ServiceExt},
BoxError, Layer, Service, ServiceBuilder,
};
use tower_http::map_response_body::MapResponseBodyLayer;

pub mod future;

// for backwards compatibility
// TODO: remove these in 0.2
#[doc(hidden)]
pub use self::future::{BoxRouteFuture, EmptyRouterFuture, RouteFuture};

/// A filter that matches one or more HTTP methods.
#[derive(Debug, Copy, Clone)]
pub enum MethodFilter {
Expand Down Expand Up @@ -385,63 +388,6 @@ where
}
}

pin_project! {
/// The response future for [`Route`].
#[derive(Debug)]
pub struct RouteFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>> {
#[pin] inner: RouteFutureInner<S, F, B>,
}
}

impl<S, F, B> RouteFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>,
{
pub(crate) fn a(a: Oneshot<S, Request<B>>) -> Self {
RouteFuture {
inner: RouteFutureInner::A { a },
}
}

pub(crate) fn b(b: Oneshot<F, Request<B>>) -> Self {
RouteFuture {
inner: RouteFutureInner::B { b },
}
}
}

pin_project! {
#[project = RouteFutureInnerProj]
#[derive(Debug)]
enum RouteFutureInner<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>,
{
A { #[pin] a: Oneshot<S, Request<B>> },
B { #[pin] b: Oneshot<F, Request<B>> },
}
}

impl<S, F, B> Future for RouteFuture<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> {
match self.project().inner.project() {
RouteFutureInnerProj::A { a } => a.poll(cx),
RouteFutureInnerProj::B { b } => b.poll(cx),
}
}
}

#[derive(Debug)]
pub(crate) struct UrlParams(pub(crate) Vec<(ByteStr, ByteStr)>);

Expand Down Expand Up @@ -520,17 +466,11 @@ impl<B, E> Service<Request<B>> for EmptyRouter<E> {
let mut res = Response::new(crate::body::empty());
*res.status_mut() = self.status;
EmptyRouterFuture {
future: future::ok(res),
future: futures_util::future::ok(res),
}
}
}

opaque_future! {
/// Response future for [`EmptyRouter`].
pub type EmptyRouterFuture<E> =
future::Ready<Result<Response<BoxBody>, E>>;
}

#[derive(Debug, Clone)]
pub(crate) struct PathPattern(Arc<Inner>);

Expand Down Expand Up @@ -666,36 +606,6 @@ where
}
}

pin_project! {
/// The response future for [`BoxRoute`].
pub struct BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
#[pin] inner: Oneshot<MpscBuffer<BoxService<Request<B>, Response<BoxBody>, E>, Request<B>>, Request<B>>,
}
}

impl<B, E> Future for BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
type Output = Result<Response<BoxBody>, E>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx)
}
}

impl<B, E> fmt::Debug for BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BoxRouteFuture").finish()
}
}

/// A [`Service`] created from a router by applying a Tower middleware.
///
/// Created with [`RoutingDsl::layer`]. See that method for more details.
Expand Down
117 changes: 117 additions & 0 deletions src/routing/future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Future types.

use crate::{body::BoxBody, buffer::MpscBuffer};
use http::{Request, Response};
use pin_project_lite::pin_project;
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::{
util::{BoxService, Oneshot},
BoxError, Service,
};

opaque_future! {
/// Response future for [`EmptyRouter`](super::EmptyRouter).
pub type EmptyRouterFuture<E> =
futures_util::future::Ready<Result<Response<BoxBody>, E>>;
}

pin_project! {
/// The response future for [`BoxRoute`](super::BoxRoute).
pub struct BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
#[pin]
pub(super) inner: Oneshot<
MpscBuffer<
BoxService<Request<B>, Response<BoxBody>, E >,
Request<B>
>,
Request<B>,
>,
}
}

impl<B, E> Future for BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
type Output = Result<Response<BoxBody>, E>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx)
}
}

impl<B, E> fmt::Debug for BoxRouteFuture<B, E>
where
E: Into<BoxError>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BoxRouteFuture").finish()
}
}

pin_project! {
/// The response future for [`Route`](super::Route).
#[derive(Debug)]
pub struct RouteFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>
{
#[pin]
inner: RouteFutureInner<S, F, B>,
}
}

impl<S, F, B> RouteFuture<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>,
{
pub(crate) fn a(a: Oneshot<S, Request<B>>) -> Self {
RouteFuture {
inner: RouteFutureInner::A { a },
}
}

pub(crate) fn b(b: Oneshot<F, Request<B>>) -> Self {
RouteFuture {
inner: RouteFutureInner::B { b },
}
}
}

pin_project! {
#[project = RouteFutureInnerProj]
#[derive(Debug)]
enum RouteFutureInner<S, F, B>
where
S: Service<Request<B>>,
F: Service<Request<B>>,
{
A { #[pin] a: Oneshot<S, Request<B>> },
B { #[pin] b: Oneshot<F, Request<B>> },
}
}

impl<S, F, B> Future for RouteFuture<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> {
match self.project().inner.project() {
RouteFutureInnerProj::A { a } => a.poll(cx),
RouteFutureInnerProj::B { b } => b.poll(cx),
}
}
}
24 changes: 24 additions & 0 deletions src/service/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,27 @@ where
}
}
}

pin_project! {
/// Response future for [`BoxResponseBody`].
#[derive(Debug)]
pub struct BoxResponseBodyFuture<F> {
#[pin]
pub(super) future: F,
}
}

impl<F, B, E> Future for BoxResponseBodyFuture<F>
where
F: Future<Output = Result<Response<B>, E>>,
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError> + Send + Sync + 'static,
{
type Output = Result<Response<BoxBody>, E>;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let res = ready!(self.project().future.poll(cx))?;
let res = res.map(box_body);
Poll::Ready(Ok(res))
}
}
Loading