warp/filters/
any.rs

1//! A filter that matches any route.
2use std::convert::Infallible;
3use std::future::Future;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7use crate::filter::{Filter, FilterBase, Internal};
8
9/// A [`Filter`](crate::Filter) that matches any route.
10///
11/// This can be a useful building block to build new filters from,
12/// since [`Filter`] is otherwise a sealed trait.
13///
14/// # Example
15///
16/// ```
17/// use warp::Filter;
18///
19/// let route = warp::any()
20///     .map(|| {
21///         "I always return this string!"
22///     });
23/// ```
24///
25/// This could allow creating a single `impl Filter` returning a specific
26/// reply, that can then be used as the end of several different filter
27/// chains.
28///
29/// Another use case is turning some clone-able resource into a `Filter`,
30/// thus allowing to easily `and` it together with others.
31///
32/// ```
33/// use std::sync::Arc;
34/// use warp::Filter;
35///
36/// let state = Arc::new(vec![33, 41]);
37/// let with_state = warp::any().map(move || state.clone());
38///
39/// // Now we could `and` with any other filter:
40///
41/// let route = warp::path::param()
42///     .and(with_state)
43///     .map(|param_id: u32, db: Arc<Vec<u32>>| {
44///         db.contains(&param_id)
45///     });
46/// ```
47pub fn any() -> impl Filter<Extract = (), Error = Infallible> + Copy {
48    Any
49}
50
51#[derive(Copy, Clone)]
52#[allow(missing_debug_implementations)]
53struct Any;
54
55impl FilterBase for Any {
56    type Extract = ();
57    type Error = Infallible;
58    type Future = AnyFut;
59
60    #[inline]
61    fn filter(&self, _: Internal) -> Self::Future {
62        AnyFut
63    }
64}
65
66#[allow(missing_debug_implementations)]
67struct AnyFut;
68
69impl Future for AnyFut {
70    type Output = Result<(), Infallible>;
71
72    #[inline]
73    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
74        Poll::Ready(Ok(()))
75    }
76}