11// #![deny(warnings)]  // FIXME: https://github.com/rust-lang/rust/issues/62411 
22#![ warn( rust_2018_idioms) ]  
33
4+ use  bytes:: Bytes ; 
5+ use  http_body_util:: { combinators:: BoxBody ,  BodyExt ,  Empty ,  Full } ; 
46use  hyper:: server:: conn:: Http ; 
57use  hyper:: service:: service_fn; 
68use  hyper:: { Body ,  Method ,  Request ,  Response ,  StatusCode } ; 
79use  tokio:: net:: TcpListener ; 
810
911use  std:: collections:: HashMap ; 
12+ use  std:: convert:: Infallible ; 
1013use  std:: net:: SocketAddr ; 
1114use  url:: form_urlencoded; 
1215
@@ -15,9 +18,11 @@ static MISSING: &[u8] = b"Missing field";
1518static  NOTNUMERIC :  & [ u8 ]  = b"Number field is not numeric" ; 
1619
1720// Using service_fn, we can turn this function into a `Service`. 
18- async  fn  param_example ( req :  Request < Body > )  -> Result < Response < Body > ,  hyper:: Error >  { 
21+ async  fn  param_example ( 
22+     req :  Request < Body > , 
23+ )  -> Result < Response < BoxBody < Bytes ,  Infallible > > ,  hyper:: Error >  { 
1924    match  ( req. method ( ) ,  req. uri ( ) . path ( ) )  { 
20-         ( & Method :: GET ,  "/" )  | ( & Method :: GET ,  "/post" )  => Ok ( Response :: new ( INDEX . into ( ) ) ) , 
25+         ( & Method :: GET ,  "/" )  | ( & Method :: GET ,  "/post" )  => Ok ( Response :: new ( full ( INDEX ) ) ) , 
2126        ( & Method :: POST ,  "/post" )  => { 
2227            // Concatenate the body... 
2328            let  b = hyper:: body:: to_bytes ( req) . await ?; 
@@ -43,7 +48,7 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
4348            }  else  { 
4449                return  Ok ( Response :: builder ( ) 
4550                    . status ( StatusCode :: UNPROCESSABLE_ENTITY ) 
46-                     . body ( MISSING . into ( ) ) 
51+                     . body ( full ( MISSING ) ) 
4752                    . unwrap ( ) ) ; 
4853            } ; 
4954            let  number = if  let  Some ( n)  = params. get ( "number" )  { 
@@ -52,13 +57,13 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
5257                }  else  { 
5358                    return  Ok ( Response :: builder ( ) 
5459                        . status ( StatusCode :: UNPROCESSABLE_ENTITY ) 
55-                         . body ( NOTNUMERIC . into ( ) ) 
60+                         . body ( full ( NOTNUMERIC ) ) 
5661                        . unwrap ( ) ) ; 
5762                } 
5863            }  else  { 
5964                return  Ok ( Response :: builder ( ) 
6065                    . status ( StatusCode :: UNPROCESSABLE_ENTITY ) 
61-                     . body ( MISSING . into ( ) ) 
66+                     . body ( full ( MISSING ) ) 
6267                    . unwrap ( ) ) ; 
6368            } ; 
6469
@@ -69,15 +74,15 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
6974            // responses such as InternalServiceError may be 
7075            // needed here, too. 
7176            let  body = format ! ( "Hello {}, your number is {}" ,  name,  number) ; 
72-             Ok ( Response :: new ( body . into ( ) ) ) 
77+             Ok ( Response :: new ( full ( body ) ) ) 
7378        } 
7479        ( & Method :: GET ,  "/get" )  => { 
7580            let  query = if  let  Some ( q)  = req. uri ( ) . query ( )  { 
7681                q
7782            }  else  { 
7883                return  Ok ( Response :: builder ( ) 
7984                    . status ( StatusCode :: UNPROCESSABLE_ENTITY ) 
80-                     . body ( MISSING . into ( ) ) 
85+                     . body ( full ( MISSING ) ) 
8186                    . unwrap ( ) ) ; 
8287            } ; 
8388            let  params = form_urlencoded:: parse ( query. as_bytes ( ) ) 
@@ -88,19 +93,27 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
8893            }  else  { 
8994                return  Ok ( Response :: builder ( ) 
9095                    . status ( StatusCode :: UNPROCESSABLE_ENTITY ) 
91-                     . body ( MISSING . into ( ) ) 
96+                     . body ( full ( MISSING ) ) 
9297                    . unwrap ( ) ) ; 
9398            } ; 
9499            let  body = format ! ( "You requested {}" ,  page) ; 
95-             Ok ( Response :: new ( body . into ( ) ) ) 
100+             Ok ( Response :: new ( full ( body ) ) ) 
96101        } 
97102        _ => Ok ( Response :: builder ( ) 
98103            . status ( StatusCode :: NOT_FOUND ) 
99-             . body ( Body :: empty ( ) ) 
104+             . body ( empty ( ) ) 
100105            . unwrap ( ) ) , 
101106    } 
102107} 
103108
109+ fn  empty ( )  -> BoxBody < Bytes ,  Infallible >  { 
110+     Empty :: < Bytes > :: new ( ) . boxed ( ) 
111+ } 
112+ 
113+ fn  full < T :  Into < Bytes > > ( chunk :  T )  -> BoxBody < Bytes ,  Infallible >  { 
114+     Full :: new ( chunk. into ( ) ) . boxed ( ) 
115+ } 
116+ 
104117#[ tokio:: main]  
105118async  fn  main ( )  -> Result < ( ) ,  Box < dyn  std:: error:: Error  + Send  + Sync > >  { 
106119    pretty_env_logger:: init ( ) ; 
0 commit comments