diff --git a/src/filters/any.rs b/src/filters/any.rs index 7328b8a6..787cd2f3 100644 --- a/src/filters/any.rs +++ b/src/filters/any.rs @@ -44,6 +44,9 @@ use crate::filter::{Filter, FilterBase, Internal}; /// db.contains(¶m_id) /// }); /// ``` +/// +/// Note that using [with_cloneable](crate::filters::cloneable::with_cloneable) can be used for +/// this specific use-case of passing along a cloneable parameter. pub fn any() -> impl Filter + Copy { Any } diff --git a/src/filters/cloneable.rs b/src/filters/cloneable.rs new file mode 100644 index 00000000..f80881bb --- /dev/null +++ b/src/filters/cloneable.rs @@ -0,0 +1,30 @@ +//! A filter that matches any route and supplies a clone of the given +//! [Cloneable](Clone) +use crate::Filter; +use std::convert::Infallible; + +/// A [`Filter`](Filter) that matches any route and yields a clone +/// of the given [Cloneable](Clone). +/// +/// This can be used to supply services or tools to the handling methods. +/// +/// # Example +/// +/// ``` +/// use std::sync::Arc; +/// use warp::Filter; +/// +/// let state = Arc::new(vec![33, 41]); +/// let with_state = warp::with_cloneable(state); +/// +/// let route = warp::path::param() +/// .and(with_state) +/// .map(|param_id: u32, db: Arc>| { +/// db.contains(¶m_id) +/// }); +/// ``` +pub fn with_cloneable( + value: C, +) -> impl Filter + Clone { + crate::any().map(move || value.clone()) +} diff --git a/src/filters/mod.rs b/src/filters/mod.rs index bd1c4871..4cb653bd 100644 --- a/src/filters/mod.rs +++ b/src/filters/mod.rs @@ -6,6 +6,7 @@ pub mod addr; pub mod any; pub mod body; +pub mod cloneable; #[cfg(any(feature = "compression-brotli", feature = "compression-gzip"))] pub mod compression; pub mod cookie; diff --git a/src/lib.rs b/src/lib.rs index f8d34566..850459f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,6 +121,8 @@ pub use self::filters::{ // any() function any::any, body, + // with_cloneable() function + cloneable::with_cloneable, cookie, // cookie() function cookie::cookie,