@@ -5,10 +5,13 @@ pub mod user_sessions;
55pub mod wrap_fn;
66
77// <simple>
8+ use std:: pin:: Pin ;
9+ use std:: task:: { Context , Poll } ;
10+
811use actix_service:: { Service , Transform } ;
912use actix_web:: { dev:: ServiceRequest , dev:: ServiceResponse , Error } ;
10- use futures:: future:: { ok, FutureResult } ;
11- use futures:: { Future , Poll } ;
13+ use futures:: future:: { ok, Ready } ;
14+ use futures:: Future ;
1215
1316// There are two steps in middleware processing.
1417// 1. Middleware initialization, middleware factory gets called with
3033 type Error = Error ;
3134 type InitError = ( ) ;
3235 type Transform = SayHiMiddleware < S > ;
33- type Future = FutureResult < Self :: Transform , Self :: InitError > ;
36+ type Future = Ready < Result < Self :: Transform , Self :: InitError > > ;
3437
3538 fn new_transform ( & self , service : S ) -> Self :: Future {
3639 ok ( SayHiMiddleware { service } )
@@ -50,34 +53,40 @@ where
5053 type Request = ServiceRequest ;
5154 type Response = ServiceResponse < B > ;
5255 type Error = Error ;
53- type Future = Box < dyn Future < Item = Self :: Response , Error = Self :: Error > > ;
56+ type Future = Pin < Box < dyn Future < Output = Result < Self :: Response , Self :: Error > > > > ;
5457
55- fn poll_ready ( & mut self ) -> Poll < ( ) , Self :: Error > {
56- self . service . poll_ready ( )
58+ fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
59+ self . service . poll_ready ( cx )
5760 }
5861
5962 fn call ( & mut self , req : ServiceRequest ) -> Self :: Future {
6063 println ! ( "Hi from start. You requested: {}" , req. path( ) ) ;
6164
62- Box :: new ( self . service . call ( req) . and_then ( |res| {
65+ let fut = self . service . call ( req) ;
66+
67+ Box :: pin ( async move {
68+ let res = fut. await ?;
69+
6370 println ! ( "Hi from response" ) ;
6471 Ok ( res)
65- } ) )
72+ } )
6673 }
6774}
6875// </simple>
6976
70- fn main ( ) {
77+ #[ actix_rt:: main]
78+ async fn main ( ) -> std:: io:: Result < ( ) > {
7179 use actix_web:: { web, App , HttpServer } ;
7280
7381 HttpServer :: new ( || {
7482 App :: new ( ) . wrap ( SayHi ) . service (
7583 web:: resource ( "/" )
76- . to ( || "Hello, middleware! Check the console where the server is run." ) ,
84+ . to ( || async {
85+ "Hello, middleware! Check the console where the server is run."
86+ } ) ,
7787 )
7888 } )
79- . bind ( "127.0.0.1:8088" )
80- . unwrap ( )
89+ . bind ( "127.0.0.1:8088" ) ?
8190 . run ( )
82- . unwrap ( ) ;
91+ . await
8392}
0 commit comments