Skip to content

Commit

Permalink
Add Internal argument to sealed Filter methods (#351)
Browse files Browse the repository at this point in the history
Closes #219
  • Loading branch information
seanmonstar committed Dec 27, 2019
1 parent 71b162c commit e308f14
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 78 deletions.
8 changes: 4 additions & 4 deletions src/filter/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::ready;
use pin_project::{pin_project, project};

use super::{Combine, Filter, FilterBase, HList, Tuple};
use super::{Combine, Filter, FilterBase, HList, Internal, Tuple};
use crate::reject::CombineRejection;

#[derive(Clone, Copy, Debug)]
Expand All @@ -27,9 +27,9 @@ where
type Error = <U::Error as CombineRejection<T::Error>>::One;
type Future = AndFuture<T, U>;

fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
AndFuture {
state: State::First(self.first.filter(), self.second.clone()),
state: State::First(self.first.filter(Internal), self.second.clone()),
}
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ where
#[project]
let (ex1, fut2) = match pin.state.project() {
State::First(first, second) => match ready!(first.poll(cx)) {
Ok(first) => (first, second.filter()),
Ok(first) => (first, second.filter(Internal)),
Err(err) => return Poll::Ready(Err(From::from(err))),
},
State::Second(ex1, second) => {
Expand Down
6 changes: 3 additions & 3 deletions src/filter/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::{pin_project, project};

use super::{Filter, FilterBase, Func};
use super::{Filter, FilterBase, Func, Internal};
use crate::reject::CombineRejection;

#[derive(Clone, Copy, Debug)]
Expand All @@ -25,9 +25,9 @@ where
type Error = <<F::Output as TryFuture>::Error as CombineRejection<T::Error>>::One;
type Future = AndThenFuture<T, F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
AndThenFuture {
state: State::First(self.filter.filter(), self.callback.clone()),
state: State::First(self.filter.filter(Internal), self.callback.clone()),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/filter/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use futures::TryFutureExt;

use super::{Filter, FilterBase, Tuple};
use super::{Filter, FilterBase, Internal, Tuple};
use crate::reject::Rejection;

/// A type representing a boxed `Filter` trait object.
Expand Down Expand Up @@ -46,7 +46,7 @@ impl<T: Tuple + Send> BoxedFilter<T> {
{
BoxedFilter {
filter: Arc::new(BoxingFilter {
filter: filter.map_err(Into::into),
filter: filter.map_err(super::Internal, Into::into),
}),
}
}
Expand Down Expand Up @@ -76,8 +76,8 @@ impl<T: Tuple + Send> FilterBase for BoxedFilter<T> {
type Error = Rejection;
type Future = Pin<Box<dyn Future<Output = Result<T, Rejection>> + Send>>;

fn filter(&self) -> Self::Future {
self.filter.filter()
fn filter(&self, _: Internal) -> Self::Future {
self.filter.filter(Internal)
}
}

Expand All @@ -94,7 +94,7 @@ where
type Error = F::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Extract, Self::Error>> + Send>>;

fn filter(&self) -> Self::Future {
Box::pin(self.filter.filter().into_future())
fn filter(&self, _: Internal) -> Self::Future {
Box::pin(self.filter.filter(Internal).into_future())
}
}
6 changes: 3 additions & 3 deletions src/filter/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::pin_project;

use super::{Filter, FilterBase, Func};
use super::{Filter, FilterBase, Func, Internal};

#[derive(Clone, Copy, Debug)]
pub struct Map<T, F> {
Expand All @@ -22,9 +22,9 @@ where
type Error = T::Error;
type Future = MapFuture<T, F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
MapFuture {
extract: self.filter.filter(),
extract: self.filter.filter(Internal),
callback: self.callback.clone(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/filter/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::TryFuture;
use pin_project::pin_project;

use super::{Filter, FilterBase};
use super::{Filter, FilterBase, Internal};
use crate::reject::IsReject;

#[derive(Clone, Copy, Debug)]
Expand All @@ -24,9 +24,9 @@ where
type Error = E;
type Future = MapErrFuture<T, F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
MapErrFuture {
extract: self.filter.filter(),
extract: self.filter.filter(Internal),
callback: self.callback.clone(),
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub trait FilterBase {
type Error: IsReject;
type Future: Future<Output = Result<Self::Extract, Self::Error>> + Send;

fn filter(&self) -> Self::Future;
fn filter(&self, internal: Internal) -> Self::Future;

fn map_err<F, E>(self, fun: F) -> MapErr<Self, F>
fn map_err<F, E>(self, _internal: Internal, fun: F) -> MapErr<Self, F>
where
Self: Sized,
F: Fn(Self::Error) -> E + Clone,
Expand All @@ -54,20 +54,18 @@ pub trait FilterBase {
}
}

/// This just makes use of rustdoc's ability to make compile_fail tests.
/// This is specifically testing to make sure `Filter::filter` isn't
/// able to be called from outside the crate (since rustdoc tests are
/// compiled as new crates).
///
/// ```compile_fail
/// use warp::Filter;
///
/// let _ = warp::any().filter();
/// ```
pub fn __warp_filter_compilefail_doctest() {
// Duplicate code to make sure the code is otherwise valid.
let _ = crate::any().filter();
}
// A crate-private argument to prevent users from calling methods on
// the `FilterBase` trait.
//
// For instance, this innocent user code could otherwise call `filter`:
//
// ```
// async fn with_filter<F: Filter>(f: F) -> Result<F::Extract, F::Error> {
// f.filter().await
// }
// ```
#[allow(missing_debug_implementations)]
pub struct Internal;

/// Composable request filters.
///
Expand Down Expand Up @@ -456,7 +454,7 @@ where
Pin<Box<dyn Future<Output = Result<Self::Extract, Self::Error>> + Send + 'static>>;

#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
Box::pin(route::with(|route| (self.func)(route)).into_future())
}
}
8 changes: 4 additions & 4 deletions src/filter/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::{pin_project, project};

use super::{Filter, FilterBase};
use super::{Filter, FilterBase, Internal};
use crate::generic::Either;
use crate::reject::CombineRejection;
use crate::route;
Expand All @@ -26,10 +26,10 @@ where
type Error = <U::Error as CombineRejection<T::Error>>::Combined;
type Future = EitherFuture<T, U>;

fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
let idx = route::with(|route| route.matched_path_index());
EitherFuture {
state: State::First(self.first.filter(), self.second.clone()),
state: State::First(self.first.filter(Internal), self.second.clone()),
original_path_index: PathIndex(idx),
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ where
}
Err(e) => {
pin.original_path_index.reset_path();
(e, second.filter())
(e, second.filter(Internal))
}
},
State::Second(err1, second) => {
Expand Down
6 changes: 3 additions & 3 deletions src/filter/or_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::{pin_project, project};

use super::{Filter, FilterBase, Func};
use super::{Filter, FilterBase, Func, Internal};
use crate::route;

#[derive(Clone, Copy, Debug)]
Expand All @@ -24,10 +24,10 @@ where
type Error = <F::Output as TryFuture>::Error;
type Future = OrElseFuture<T, F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
let idx = route::with(|route| route.matched_path_index());
OrElseFuture {
state: State::First(self.filter.filter(), self.callback.clone()),
state: State::First(self.filter.filter(Internal), self.callback.clone()),
original_path_index: PathIndex(idx),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/filter/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::{pin_project, project};

use super::{Filter, FilterBase, Func};
use super::{Filter, FilterBase, Func, Internal};
use crate::generic::Either;
use crate::route;

Expand All @@ -25,10 +25,10 @@ where
type Error = <F::Output as TryFuture>::Error;
type Future = RecoverFuture<T, F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
let idx = route::with(|route| route.matched_path_index());
RecoverFuture {
state: State::First(self.filter.filter(), self.callback.clone()),
state: State::First(self.filter.filter(Internal), self.callback.clone()),
original_path_index: PathIndex(idx),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/filter/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where
debug_assert!(!route::is_set(), "nested route::set calls");

let route = Route::new(req, remote_addr);
let fut = route::set(&route, || self.filter.filter());
let fut = route::set(&route, || self.filter.filter(super::Internal));
FilteredFuture { future: fut, route }
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/filter/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::pin_project;

use super::{Either, Filter, FilterBase, Tuple};
use super::{Either, Filter, FilterBase, Internal, Tuple};

#[derive(Clone, Copy, Debug)]
pub struct Unify<F> {
Expand All @@ -21,9 +21,9 @@ where
type Error = F::Error;
type Future = UnifyFuture<F::Future>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
UnifyFuture {
inner: self.filter.filter(),
inner: self.filter.filter(Internal),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/filter/untuple_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use futures::{ready, TryFuture};
use pin_project::pin_project;

use super::{Filter, FilterBase, Tuple};
use super::{Filter, FilterBase, Internal, Tuple};

#[derive(Clone, Copy, Debug)]
pub struct UntupleOne<F> {
Expand All @@ -21,9 +21,9 @@ where
type Error = F::Error;
type Future = UntupleOneFuture<F>;
#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
UntupleOneFuture {
extract: self.filter.filter(),
extract: self.filter.filter(Internal),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/filters/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::filter::{Filter, FilterBase};
use crate::filter::{Filter, FilterBase, Internal};

/// A filter that matches any route.
///
Expand Down Expand Up @@ -58,7 +58,7 @@ impl FilterBase for Any {
type Future = AnyFut;

#[inline]
fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
AnyFut
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/filters/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) fn body() -> impl Filter<Extract = (Body,), Error = Rejection> + Copy
/// ```
pub fn content_length_limit(limit: u64) -> impl Filter<Extract = (), Error = Rejection> + Copy {
crate::filters::header::header2()
.map_err(|_| {
.map_err(crate::filter::Internal, |_| {
log::debug!("content-length missing");
reject::length_required()
})
Expand Down
8 changes: 4 additions & 4 deletions src/filters/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ mod internal {
use pin_project::pin_project;

use super::{Configured, CorsForbidden, Validated};
use crate::filter::{Filter, FilterBase, One};
use crate::filter::{Filter, FilterBase, Internal, One};
use crate::generic::Either;
use crate::reject::{CombineRejection, Rejection};
use crate::route;
Expand All @@ -443,7 +443,7 @@ mod internal {
WrappedFuture<F::Future>,
>;

fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
let validated =
route::with(|route| self.config.check_request(route.method(), route.headers()));

Expand All @@ -456,11 +456,11 @@ mod internal {
future::Either::Left(future::ok((Either::A((preflight,)),)))
}
Ok(Validated::Simple(origin)) => future::Either::Right(WrappedFuture {
inner: self.inner.filter(),
inner: self.inner.filter(Internal),
wrapped: Some((self.config.clone(), origin)),
}),
Ok(Validated::NotCors) => future::Either::Right(WrappedFuture {
inner: self.inner.filter(),
inner: self.inner.filter(Internal),
wrapped: None,
}),
Err(err) => {
Expand Down
6 changes: 3 additions & 3 deletions src/filters/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod internal {
use pin_project::pin_project;

use super::{Info, Log};
use crate::filter::{Filter, FilterBase};
use crate::filter::{Filter, FilterBase, Internal};
use crate::reject::IsReject;
use crate::reply::{Reply, Response};
use crate::route;
Expand Down Expand Up @@ -219,11 +219,11 @@ mod internal {
type Error = F::Error;
type Future = WithLogFuture<FN, F::Future>;

fn filter(&self) -> Self::Future {
fn filter(&self, _: Internal) -> Self::Future {
let started = tokio::time::Instant::now().into_std();
WithLogFuture {
log: self.log.clone(),
future: self.filter.filter(),
future: self.filter.filter(Internal),
started,
}
}
Expand Down

0 comments on commit e308f14

Please sign in to comment.