-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide with_cloneable to simplify passing shared services to routes
with_cloneable is essentially a shortcut for: crate::any().map(move || value.clone())
- Loading branch information
Showing
4 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Vec<u32>>| { | ||
/// db.contains(¶m_id) | ||
/// }); | ||
/// ``` | ||
pub fn with_cloneable<C: Clone + Send>( | ||
value: C, | ||
) -> impl Filter<Extract = (C,), Error = Infallible> + Clone { | ||
crate::any().map(move || value.clone()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters