Move response futures into their own modules (#130)

It cleans up the docs to have the futures in their own modules as users
are unlikely to look at them. Also matches the pattern used in tower
https://docs.rs/tower/0.4.8/tower/util/future/index.html.

Added re-exports to the old locations so its not a breaking change.
This commit is contained in:
David Pedersen
2021-08-06 01:15:10 +02:00
committed by GitHub
parent 68f826ef3b
commit d4ce90e2e6
5 changed files with 168 additions and 132 deletions
+9 -99
View File
@@ -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 {
@@ -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)>);
@@ -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>);
@@ -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.