This commit is contained in:
nora 2023-03-07 16:59:20 +01:00
parent b4e7136ad9
commit 1d163b6fef
5 changed files with 50 additions and 555 deletions

View file

@ -27,16 +27,12 @@ where
loop {} loop {}
} }
} }
pub trait MakeServiceRef<Target, ReqBody>: self::sealed::Sealed<(Target, ReqBody)> { pub trait MakeServiceRef<Target, ReqBody> {
type ResBody: HttpBody; type ResBody;
type Error: Into<Box<dyn StdError + Send + Sync>>; type Error;
type Service: HttpService<ReqBody, ResBody = Self::ResBody, Error = Self::Error>; type Service: HttpService<ReqBody, ResBody = Self::ResBody, Error = Self::Error>;
type MakeError: Into<Box<dyn StdError + Send + Sync>>;
type Future: Future<Output = Result<Self::Service, Self::MakeError>>;
type __DontNameMe: self::sealed::CantImpl;
fn poll_ready_ref(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::MakeError>>;
fn make_service_ref(&mut self, target: &Target) -> Self::Future;
} }
impl<T, Target, E, ME, S, F, IB, OB> MakeServiceRef<Target, IB> for T impl<T, Target, E, ME, S, F, IB, OB> MakeServiceRef<Target, IB> for T
where where
T: for<'a> Service<&'a Target, Error = ME, Response = S, Future = F>, T: for<'a> Service<&'a Target, Error = ME, Response = S, Future = F>,
@ -50,15 +46,6 @@ where
type Error = E; type Error = E;
type Service = S; type Service = S;
type ResBody = OB; type ResBody = OB;
type MakeError = ME;
type Future = F;
type __DontNameMe = self::sealed::CantName;
fn poll_ready_ref(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::MakeError>> {
loop {}
}
fn make_service_ref(&mut self, target: &Target) -> Self::Future {
loop {}
}
} }
impl<T, Target, S, B1, B2> self::sealed::Sealed<(Target, B1)> for T impl<T, Target, S, B1, B2> self::sealed::Sealed<(Target, B1)> for T
where where

View file

@ -1,5 +1,4 @@
use std::convert::Infallible; use std::convert::Infallible;
use std::net::SocketAddr;
use futures::future; use futures::future;
use hyper::service::make_service_fn; use hyper::service::make_service_fn;

View file

@ -11,11 +11,6 @@ mod then;
mod unify; mod unify;
mod untuple_one; mod untuple_one;
mod wrap; mod wrap;
use std::future::Future;
use futures_util::{future, TryFuture, TryFutureExt};
pub(crate) use crate::generic::{one, Combine, Either, Func, One, Tuple};
use crate::reject::{CombineRejection, IsReject, Rejection};
use crate::route::{Route};
pub(crate) use self::and::And; pub(crate) use self::and::And;
use self::and_then::AndThen; use self::and_then::AndThen;
pub use self::boxed::BoxedFilter; pub use self::boxed::BoxedFilter;
@ -29,6 +24,11 @@ use self::unify::Unify;
use self::untuple_one::UntupleOne; use self::untuple_one::UntupleOne;
pub use self::wrap::wrap_fn; pub use self::wrap::wrap_fn;
pub(crate) use self::wrap::{Wrap, WrapSealed}; pub(crate) use self::wrap::{Wrap, WrapSealed};
pub(crate) use crate::generic::{one, Combine, Either, Func, One, Tuple};
use crate::reject::{CombineRejection, IsReject, Rejection};
use crate::route::Route;
use futures_util::{future, TryFuture, TryFutureExt};
use std::future::Future;
pub trait FilterBase { pub trait FilterBase {
type Extract: Tuple; type Extract: Tuple;
type Error: IsReject; type Error: IsReject;
@ -45,47 +45,8 @@ pub trait FilterBase {
} }
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Internal; pub struct Internal;
pub trait Filter: FilterBase { pub trait Filter: FilterBase {
fn and<F>(self, other: F) -> And<Self, F> fn and<F>(self, other: F) -> And<Self, F>
where where
Self: Sized, Self: Sized,
@ -95,18 +56,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn or<F>(self, other: F) -> Or<Self, F> fn or<F>(self, other: F) -> Or<Self, F>
where where
Self: Filter<Error = Rejection> + Sized, Self: Filter<Error = Rejection> + Sized,
@ -115,47 +65,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn map<F>(self, fun: F) -> Map<Self, F> fn map<F>(self, fun: F) -> Map<Self, F>
where where
Self: Sized, Self: Sized,
@ -163,21 +73,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn then<F>(self, fun: F) -> Then<Self, F> fn then<F>(self, fun: F) -> Then<Self, F>
where where
Self: Sized, Self: Sized,
@ -186,32 +82,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn and_then<F>(self, fun: F) -> AndThen<Self, F> fn and_then<F>(self, fun: F) -> AndThen<Self, F>
where where
Self: Sized, Self: Sized,
@ -221,10 +92,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn or_else<F>(self, fun: F) -> OrElse<Self, F> fn or_else<F>(self, fun: F) -> OrElse<Self, F>
where where
Self: Filter<Error = Rejection> + Sized, Self: Filter<Error = Rejection> + Sized,
@ -234,13 +102,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn recover<F>(self, fun: F) -> Recover<Self, F> fn recover<F>(self, fun: F) -> Recover<Self, F>
where where
Self: Filter<Error = Rejection> + Sized, Self: Filter<Error = Rejection> + Sized,
@ -250,28 +112,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn unify<T>(self) -> Unify<Self> fn unify<T>(self) -> Unify<Self>
where where
Self: Filter<Extract = (Either<T, T>,)> + Sized, Self: Filter<Extract = (Either<T, T>,)> + Sized,
@ -279,41 +120,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn untuple_one<T>(self) -> UntupleOne<Self> fn untuple_one<T>(self) -> UntupleOne<Self>
where where
Self: Filter<Extract = (T,)> + Sized, Self: Filter<Extract = (T,)> + Sized,
@ -321,22 +128,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn with<W>(self, wrapper: W) -> W::Wrapped fn with<W>(self, wrapper: W) -> W::Wrapped
where where
Self: Sized, Self: Sized,
@ -344,30 +136,7 @@ pub trait Filter: FilterBase {
{ {
loop {} loop {}
} }
fn boxed(self) -> BoxedFilter<Self::Extract> fn boxed(self) -> BoxedFilter<Self::Extract>
where where
Self: Sized + Send + Sync + 'static, Self: Sized + Send + Sync + 'static,

View file

@ -124,41 +124,15 @@
//! like `body` or `headers`. If a different type of filter comes first, a request //! like `body` or `headers`. If a different type of filter comes first, a request
//! with an invalid body for route `/right-path-wrong-body` may try matching against `/wrong-path` //! with an invalid body for route `/right-path-wrong-body` may try matching against `/wrong-path`
//! and return the error from `/wrong-path` instead of the correct body-related error. //! and return the error from `/wrong-path` instead of the correct body-related error.
use std::convert::Infallible;
use std::fmt;
use std::str::FromStr;
use futures_util::future;
use http::uri::PathAndQuery;
use self::internal::Opaque; use self::internal::Opaque;
use crate::filter::{filter_fn, one, Filter, FilterBase, Internal, One, Tuple}; use crate::filter::{filter_fn, one, Filter, FilterBase, Internal, One, Tuple};
use crate::reject::{self, Rejection}; use crate::reject::{self, Rejection};
use crate::route::Route; use crate::route::Route;
use futures_util::future;
use http::uri::PathAndQuery;
use std::convert::Infallible;
use std::fmt;
use std::str::FromStr;
pub fn path<P>(p: P) -> Exact<Opaque<P>> pub fn path<P>(p: P) -> Exact<Opaque<P>>
where where
@ -167,8 +141,6 @@ where
loop {} loop {}
} }
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Exact<P>(P); pub struct Exact<P>(P);
@ -185,19 +157,6 @@ where
} }
} }
pub fn end() -> impl Filter<Extract = (), Error = Rejection> + Copy { pub fn end() -> impl Filter<Extract = (), Error = Rejection> + Copy {
filter_fn(move |route| { filter_fn(move |route| {
if route.path().is_empty() { if route.path().is_empty() {
@ -208,27 +167,8 @@ pub fn end() -> impl Filter<Extract = (), Error = Rejection> + Copy {
}) })
} }
pub fn param<T: FromStr + Send + 'static>(
) -> impl Filter<Extract = One<T>, Error = Rejection> + Copy {
pub fn param<T: FromStr + Send + 'static>() -> impl Filter<
Extract = One<T>,
Error = Rejection,
> + Copy {
filter_segment(|seg| { filter_segment(|seg| {
tracing::trace!("param?: {:?}", seg); tracing::trace!("param?: {:?}", seg);
if seg.is_empty() { if seg.is_empty() {
@ -238,29 +178,16 @@ pub fn param<T: FromStr + Send + 'static>() -> impl Filter<
}) })
} }
pub fn tail() -> impl Filter<Extract = One<Tail>, Error = Infallible> + Copy { pub fn tail() -> impl Filter<Extract = One<Tail>, Error = Infallible> + Copy {
filter_fn(move |route| { filter_fn(move |route| {
let path = path_and_query(route); let path = path_and_query(route);
let idx = route.matched_path_index(); let idx = route.matched_path_index();
let end = path.path().len() - idx; let end = path.path().len() - idx;
route.set_unmatched_path(end); route.set_unmatched_path(end);
future::ok(one(Tail { path, start_index: idx })) future::ok(one(Tail {
path,
start_index: idx,
}))
}) })
} }
@ -269,7 +196,6 @@ pub struct Tail {
start_index: usize, start_index: usize,
} }
impl Tail { impl Tail {
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
loop {} loop {}
} }
@ -280,28 +206,14 @@ impl fmt::Debug for Tail {
} }
} }
pub fn peek() -> impl Filter<Extract = One<Peek>, Error = Infallible> + Copy { pub fn peek() -> impl Filter<Extract = One<Peek>, Error = Infallible> + Copy {
filter_fn(move |route| { filter_fn(move |route| {
let path = path_and_query(route); let path = path_and_query(route);
let idx = route.matched_path_index(); let idx = route.matched_path_index();
future::ok(one(Peek { path, start_index: idx })) future::ok(one(Peek {
path,
start_index: idx,
}))
}) })
} }
@ -310,11 +222,10 @@ pub struct Peek {
start_index: usize, start_index: usize,
} }
impl Peek { impl Peek {
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
loop {} loop {}
} }
pub fn segments(&self) -> impl Iterator<Item = &str> { pub fn segments(&self) -> impl Iterator<Item = &str> {
self.as_str().split('/').filter(|seg| !seg.is_empty()) self.as_str().split('/').filter(|seg| !seg.is_empty())
} }
@ -325,43 +236,12 @@ impl fmt::Debug for Peek {
} }
} }
pub fn full() -> impl Filter<Extract = One<FullPath>, Error = Infallible> + Copy { pub fn full() -> impl Filter<Extract = One<FullPath>, Error = Infallible> + Copy {
filter_fn(move |route| future::ok(one(FullPath(path_and_query(route))))) filter_fn(move |route| future::ok(one(FullPath(path_and_query(route)))))
} }
pub struct FullPath(PathAndQuery); pub struct FullPath(PathAndQuery);
impl FullPath { impl FullPath {
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
loop {} loop {}
} }
@ -388,60 +268,6 @@ fn path_and_query(route: &Route) -> PathAndQuery {
loop {} loop {}
} }
#[macro_export] #[macro_export]
macro_rules! path { macro_rules! path {
($($pieces:tt)*) => { ($($pieces:tt)*) => {
@ -487,24 +313,6 @@ macro_rules! __internal_path {
}; };
} }
fn _path_macro_compile_fail() {} fn _path_macro_compile_fail() {}
mod internal { mod internal {
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]

View file

@ -8,59 +8,19 @@
//! //!
//! [`tracing`]: https://crates.io/crates/tracing //! [`tracing`]: https://crates.io/crates/tracing
//! [`Spans`]: https://docs.rs/tracing/latest/tracing/#spans //! [`Spans`]: https://docs.rs/tracing/latest/tracing/#spans
use tracing::Span; use self::internal::WithTrace;
use std::net::SocketAddr;
use http::{self};
use crate::filter::{Filter, WrapSealed}; use crate::filter::{Filter, WrapSealed};
use crate::reject::IsReject; use crate::reject::IsReject;
use crate::reply::Reply; use crate::reply::Reply;
use crate::route::Route; use crate::route::Route;
use self::internal::WithTrace; use http::{self};
use std::net::SocketAddr;
use tracing::Span;
pub fn request() -> Trace<impl Fn(Info<'_>) -> Span + Clone> { pub fn request() -> Trace<impl Fn(Info<'_>) -> Span + Clone> {
trace(|info: Info<'_>| { trace(|info: Info<'_>| loop {})
loop {}
})
} }
pub fn trace<F>(func: F) -> Trace<F> pub fn trace<F>(func: F) -> Trace<F>
where where
F: Fn(Info<'_>) -> Span + Clone, F: Fn(Info<'_>) -> Span + Clone,
@ -68,37 +28,10 @@ where
loop {} loop {}
} }
pub fn named(name: &'static str) -> Trace<impl Fn(Info<'_>) -> Span + Copy> { pub fn named(name: &'static str) -> Trace<impl Fn(Info<'_>) -> Span + Copy> {
trace(move |_| tracing::debug_span!("context", "{}", name,)) trace(move |_| tracing::debug_span!("context", "{}", name,))
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Trace<F> { pub struct Trace<F> {
func: F, func: F,
@ -121,47 +54,46 @@ where
} }
} }
impl<'a> Info<'a> { impl<'a> Info<'a> {
pub fn remote_addr(&self) -> Option<SocketAddr> { pub fn remote_addr(&self) -> Option<SocketAddr> {
loop {} loop {}
} }
pub fn method(&self) -> &http::Method { pub fn method(&self) -> &http::Method {
loop {} loop {}
} }
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
loop {} loop {}
} }
pub fn version(&self) -> http::Version { pub fn version(&self) -> http::Version {
loop {} loop {}
} }
pub fn referer(&self) -> Option<&str> { pub fn referer(&self) -> Option<&str> {
loop {} loop {}
} }
pub fn user_agent(&self) -> Option<&str> { pub fn user_agent(&self) -> Option<&str> {
loop {} loop {}
} }
pub fn host(&self) -> Option<&str> { pub fn host(&self) -> Option<&str> {
loop {} loop {}
} }
pub fn request_headers(&self) -> &http::HeaderMap { pub fn request_headers(&self) -> &http::HeaderMap {
loop {} loop {}
} }
} }
mod internal { mod internal {
use futures_util::{future::Inspect, future::MapOk};
use super::{Info, Trace}; use super::{Info, Trace};
use crate::filter::{Filter, FilterBase, Internal}; use crate::filter::{Filter, FilterBase, Internal};
use crate::reject::IsReject; use crate::reject::IsReject;
use crate::reply::Reply; use crate::reply::Reply;
use crate::reply::Response; use crate::reply::Response;
use futures_util::{future::Inspect, future::MapOk};
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Traced(pub(super) Response); pub struct Traced(pub(super) Response);
impl Reply for Traced { impl Reply for Traced {
@ -176,7 +108,7 @@ mod internal {
pub(super) filter: F, pub(super) filter: F,
pub(super) trace: Trace<FN>, pub(super) trace: Trace<FN>,
} }
use tracing::instrument::{Instrumented}; use tracing::instrument::Instrumented;
use tracing::Span; use tracing::Span;
fn finished_logger<E: IsReject>(reply: &Result<(Traced,), E>) { fn finished_logger<E: IsReject>(reply: &Result<(Traced,), E>) {
loop {} loop {}