1- use std:: future:: Future ;
2- use std:: pin:: Pin ;
31use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
42use std:: sync:: Arc ;
53
@@ -25,22 +23,17 @@ impl UserDatabase {
2523// This is an example of a function middleware that uses the
2624// application state. Because it depends on a specific request state,
2725// it would likely be closely tied to a specific application
28- fn user_loader < ' a > (
29- mut request : Request < UserDatabase > ,
30- next : Next < ' a , UserDatabase > ,
31- ) -> Pin < Box < dyn Future < Output = Result > + Send + ' a > > {
32- Box :: pin ( async {
33- if let Some ( user) = request. state ( ) . find_user ( ) . await {
34- tide:: log:: trace!( "user loaded" , { user: user. name} ) ;
35- request. set_ext ( user) ;
36- Ok ( next. run ( request) . await )
37- // this middleware only needs to run before the endpoint, so
38- // it just passes through the result of Next
39- } else {
40- // do not run endpoints, we could not find a user
41- Ok ( Response :: new ( StatusCode :: Unauthorized ) )
42- }
43- } )
26+ async fn user_loader ( mut request : Request , next : Next ) -> Result {
27+ if let Some ( user) = request. state :: < UserDatabase > ( ) . find_user ( ) . await {
28+ tide:: log:: trace!( "user loaded" , { user: user. name} ) ;
29+ request. set_ext ( user) ;
30+ Ok ( next. run ( request) . await )
31+ // this middleware only needs to run before the endpoint, so
32+ // it just passes through the result of Next
33+ } else {
34+ // do not run endpoints, we could not find a user
35+ Ok ( Response :: new ( StatusCode :: Unauthorized ) )
36+ }
4437}
4538
4639// This is an example of middleware that keeps its own state and could
@@ -61,8 +54,8 @@ impl RequestCounterMiddleware {
6154struct RequestCount ( usize ) ;
6255
6356#[ tide:: utils:: async_trait]
64- impl < State : Clone + Send + Sync + ' static > Middleware < State > for RequestCounterMiddleware {
65- async fn handle ( & self , mut req : Request < State > , next : Next < ' _ , State > ) -> Result {
57+ impl Middleware for RequestCounterMiddleware {
58+ async fn handle ( & self , mut req : Request , next : Next ) -> Result {
6659 let count = self . requests_counted . fetch_add ( 1 , Ordering :: Relaxed ) ;
6760 tide:: log:: trace!( "request counter" , { count: count } ) ;
6861 req. set_ext ( RequestCount ( count) ) ;
@@ -114,12 +107,12 @@ async fn main() -> Result<()> {
114107
115108 app. with ( user_loader) ;
116109 app. with ( RequestCounterMiddleware :: new ( 0 ) ) ;
117- app. with ( Before ( |mut request : Request < UserDatabase > | async move {
110+ app. with ( Before ( |mut request : Request | async move {
118111 request. set_ext ( std:: time:: Instant :: now ( ) ) ;
119112 request
120113 } ) ) ;
121114
122- app. at ( "/" ) . get ( |req : Request < _ > | async move {
115+ app. at ( "/" ) . get ( |req : Request | async move {
123116 let count: & RequestCount = req. ext ( ) . unwrap ( ) ;
124117 let user: & User = req. ext ( ) . unwrap ( ) ;
125118
0 commit comments