File tree 2 files changed +27
-4
lines changed
2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change 1
1
use async_std:: io:: { self , BufRead , Read } ;
2
2
3
3
use std:: borrow:: Borrow ;
4
+ use std:: convert:: TryInto ;
4
5
use std:: fmt:: { self , Debug } ;
5
6
use std:: pin:: Pin ;
6
7
use std:: task:: { Context , Poll } ;
@@ -12,6 +13,16 @@ type BodyReader = dyn BufRead + Unpin + Send + 'static;
12
13
13
14
pin_project_lite:: pin_project! {
14
15
/// An HTTP response.
16
+ ///
17
+ /// ```
18
+ /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
19
+ /// #
20
+ /// use http_types::{Response, StatusCode};
21
+ ///
22
+ /// let mut req = Response::new(200)?;
23
+ /// #
24
+ /// # Ok(()) }
25
+ /// ```
15
26
pub struct Response {
16
27
#[ pin]
17
28
body_reader: Box <BodyReader >,
@@ -23,13 +34,17 @@ pin_project_lite::pin_project! {
23
34
24
35
impl Response {
25
36
/// Create a new response.
26
- pub fn new ( status : StatusCode ) -> Self {
27
- Self {
28
- status,
37
+ pub fn new < S > ( status : S ) -> Result < Self , Box < dyn std:: error:: Error + Send + Sync + ' static > >
38
+ where
39
+ S : TryInto < StatusCode > ,
40
+ <S as TryInto < StatusCode > >:: Error : Sync + Send + std:: error:: Error + ' static ,
41
+ {
42
+ Ok ( Self {
43
+ status : status. try_into ( ) ?,
29
44
headers : Headers :: new ( ) ,
30
45
body_reader : Box :: new ( io:: empty ( ) ) ,
31
46
length : Some ( 0 ) ,
32
- }
47
+ } )
33
48
}
34
49
35
50
/// Get the status
Original file line number Diff line number Diff line change @@ -466,6 +466,14 @@ impl Into<u16> for StatusCode {
466
466
}
467
467
}
468
468
469
+ impl std:: convert:: TryFrom < i32 > for StatusCode {
470
+ type Error = Box < dyn std:: error:: Error + Send + Sync + ' static > ;
471
+
472
+ fn try_from ( value : i32 ) -> Result < Self , Self :: Error > {
473
+ Self :: try_from ( value as u16 )
474
+ }
475
+ }
476
+
469
477
impl std:: convert:: TryFrom < u16 > for StatusCode {
470
478
type Error = Box < dyn std:: error:: Error + Send + Sync + ' static > ;
471
479
You can’t perform that action at this time.
0 commit comments