11use  crate :: core:: res_body:: { full,  ResBody } ; 
2+ use  crate :: { HeaderMap ,  StatusCode } ; 
23use  bytes:: Bytes ; 
34use  headers:: { Header ,  HeaderMapExt } ; 
4- use  hyper:: Response  as  HyperResponse ; 
55use  std:: fmt; 
66use  std:: fmt:: { Display ,  Formatter } ; 
7- use  std:: ops:: { Deref ,  DerefMut } ; 
87
98/// 响应体 
109/// ``` 
1110/// use silent::Response; 
1211/// let req = Response::empty(); 
1312/// ``` 
1413pub  struct  Response  { 
15-     pub ( crate )  res :  HyperResponse < ResBody > , 
14+     /// The HTTP status code. 
15+ pub ( crate )  status_code :  StatusCode , 
16+     /// The HTTP headers. 
17+ pub ( crate )  headers :  HeaderMap , 
18+     pub ( crate )  body :  ResBody , 
1619} 
1720
1821impl  fmt:: Debug  for  Response  { 
1922    #[ inline]  
2023    fn  fmt ( & self ,  f :  & mut  Formatter )  -> fmt:: Result  { 
21-         writeln ! ( f,  "HTTP/1.1 {}\n {:?}" ,  self . status ( ) ,  self . headers( ) ) 
24+         writeln ! ( f,  "HTTP/1.1 {}\n {:?}" ,  self . status_code ,  self . headers) 
2225    } 
2326} 
2427
@@ -32,53 +35,58 @@ impl Display for Response {
3235impl  Response  { 
3336    /// 创建空响应体 
3437pub  fn  empty ( )  -> Self  { 
35-         Response :: from ( Bytes :: new ( ) ) 
38+         Self  { 
39+             status_code :  StatusCode :: OK , 
40+             headers :  HeaderMap :: new ( ) , 
41+             body :  ResBody :: None , 
42+         } 
3643    } 
3744    /// 设置响应状态 
38- pub  fn  set_status ( & mut  self ,  status :  hyper :: StatusCode )  { 
39-         * self . res . status_mut ( )  = status; 
45+ pub  fn  set_status ( & mut  self ,  status :  StatusCode )  { 
46+         self . status_code  = status; 
4047    } 
4148    /// 设置响应body 
42- pub  fn  set_body ( mut  self ,  body :  ResBody )  -> Self  { 
43-         * self . res . body_mut ( )  = body; 
44-         self 
49+ pub  fn  set_body ( & mut  self ,  body :  ResBody )  { 
50+         self . body  = body; 
4551    } 
4652    /// 设置响应header 
4753pub  fn  set_header ( 
4854        mut  self , 
4955        key :  hyper:: header:: HeaderName , 
5056        value :  hyper:: header:: HeaderValue , 
5157    )  -> Self  { 
52-         self . headers_mut ( ) . insert ( key,  value) ; 
58+         self . headers . insert ( key,  value) ; 
5359        self 
5460    } 
5561    /// 设置响应header 
5662pub  fn  set_typed_header < H > ( & mut  self ,  header :  H ) 
5763    where 
5864        H :  Header , 
5965    { 
60-         self . headers_mut ( ) . typed_insert ( header) ; 
66+         self . headers . typed_insert ( header) ; 
6167    } 
62- } 
6368
64- impl < T :   Into < Bytes > >   From < T >   for   Response   { 
65-     fn  from ( chunk :   T )  -> Self  { 
66-         Self  { 
67-             res :   HyperResponse :: new ( full ( chunk ) ) , 
68-         } 
69-     } 
70- } 
69+      # [ inline ] 
70+     pub ( crate )   fn  into_hyper ( self )  -> hyper :: Response < ResBody >  { 
71+         let   Self  { 
72+             status_code , 
73+             headers , 
74+             body , 
75+          }  =  self ; 
7176
72- impl  Deref  for  Response  { 
73-     type  Target  = HyperResponse < ResBody > ; 
77+         let  mut  res = hyper:: Response :: new ( body) ; 
78+         * res. headers_mut ( )  = headers; 
79+         // Default to a 404 if no response code was set 
80+         * res. status_mut ( )  = status_code; 
7481
75-     fn  deref ( & self )  -> & Self :: Target  { 
76-         & self . res 
82+         res
7783    } 
7884} 
7985
80- impl  DerefMut  for  Response  { 
81-     fn  deref_mut ( & mut  self )  -> & mut  Self :: Target  { 
82-         & mut  self . res 
86+ impl < T :  Into < Bytes > >  From < T >  for  Response  { 
87+     fn  from ( chunk :  T )  -> Self  { 
88+         let  mut  res = Response :: empty ( ) ; 
89+         res. set_body ( full ( chunk. into ( ) ) ) ; 
90+         res
8391    } 
8492} 
0 commit comments