@@ -70,18 +70,19 @@ impl StatusCode {
70
70
/// assert!(err.is_err());
71
71
/// ```
72
72
#[ inline]
73
- pub fn from_u16 ( src : u16 ) -> Result < StatusCode , InvalidStatusCode > {
73
+ pub const fn from_u16 ( src : u16 ) -> Result < StatusCode , InvalidStatusCode > {
74
74
if src < 100 || src >= 1000 {
75
75
return Err ( InvalidStatusCode :: new ( ) ) ;
76
76
}
77
77
78
- NonZeroU16 :: new ( src)
79
- . map ( StatusCode )
80
- . ok_or_else ( InvalidStatusCode :: new)
78
+ match NonZeroU16 :: new ( src) {
79
+ Some ( code) => Ok ( StatusCode ( code) ) ,
80
+ None => Err ( InvalidStatusCode :: new ( ) ) ,
81
+ }
81
82
}
82
83
83
84
/// Converts a &[u8] to a status code
84
- pub fn from_bytes ( src : & [ u8 ] ) -> Result < StatusCode , InvalidStatusCode > {
85
+ pub const fn from_bytes ( src : & [ u8 ] ) -> Result < StatusCode , InvalidStatusCode > {
85
86
if src. len ( ) != 3 {
86
87
return Err ( InvalidStatusCode :: new ( ) ) ;
87
88
}
@@ -95,9 +96,11 @@ impl StatusCode {
95
96
}
96
97
97
98
let status = ( a * 100 ) + ( b * 10 ) + c;
98
- NonZeroU16 :: new ( status)
99
- . map ( StatusCode )
100
- . ok_or_else ( InvalidStatusCode :: new)
99
+
100
+ match NonZeroU16 :: new ( status) {
101
+ Some ( code) => Ok ( StatusCode ( code) ) ,
102
+ None => Err ( InvalidStatusCode :: new ( ) ) ,
103
+ }
101
104
}
102
105
103
106
/// Returns the `u16` corresponding to this `StatusCode`.
@@ -116,8 +119,8 @@ impl StatusCode {
116
119
/// assert_eq!(status.as_u16(), 200);
117
120
/// ```
118
121
#[ inline]
119
- pub fn as_u16 ( & self ) -> u16 {
120
- ( * self ) . into ( )
122
+ pub const fn as_u16 ( & self ) -> u16 {
123
+ self . 0 . get ( )
121
124
}
122
125
123
126
/// Returns a &str representation of the `StatusCode`
@@ -164,37 +167,37 @@ impl StatusCode {
164
167
/// let status = http::StatusCode::OK;
165
168
/// assert_eq!(status.canonical_reason(), Some("OK"));
166
169
/// ```
167
- pub fn canonical_reason ( & self ) -> Option < & ' static str > {
170
+ pub const fn canonical_reason ( & self ) -> Option < & ' static str > {
168
171
canonical_reason ( self . 0 . get ( ) )
169
172
}
170
173
171
174
/// Check if status is within 100-199.
172
175
#[ inline]
173
- pub fn is_informational ( & self ) -> bool {
176
+ pub const fn is_informational ( & self ) -> bool {
174
177
200 > self . 0 . get ( ) && self . 0 . get ( ) >= 100
175
178
}
176
179
177
180
/// Check if status is within 200-299.
178
181
#[ inline]
179
- pub fn is_success ( & self ) -> bool {
182
+ pub const fn is_success ( & self ) -> bool {
180
183
300 > self . 0 . get ( ) && self . 0 . get ( ) >= 200
181
184
}
182
185
183
186
/// Check if status is within 300-399.
184
187
#[ inline]
185
- pub fn is_redirection ( & self ) -> bool {
188
+ pub const fn is_redirection ( & self ) -> bool {
186
189
400 > self . 0 . get ( ) && self . 0 . get ( ) >= 300
187
190
}
188
191
189
192
/// Check if status is within 400-499.
190
193
#[ inline]
191
- pub fn is_client_error ( & self ) -> bool {
194
+ pub const fn is_client_error ( & self ) -> bool {
192
195
500 > self . 0 . get ( ) && self . 0 . get ( ) >= 400
193
196
}
194
197
195
198
/// Check if status is within 500-599.
196
199
#[ inline]
197
- pub fn is_server_error ( & self ) -> bool {
200
+ pub const fn is_server_error ( & self ) -> bool {
198
201
600 > self . 0 . get ( ) && self . 0 . get ( ) >= 500
199
202
}
200
203
}
@@ -309,7 +312,7 @@ macro_rules! status_codes {
309
312
310
313
}
311
314
312
- fn canonical_reason( num: u16 ) -> Option <& ' static str > {
315
+ const fn canonical_reason( num: u16 ) -> Option <& ' static str > {
313
316
match num {
314
317
$(
315
318
$num => Some ( $phrase) ,
@@ -515,7 +518,7 @@ status_codes! {
515
518
}
516
519
517
520
impl InvalidStatusCode {
518
- fn new ( ) -> InvalidStatusCode {
521
+ const fn new ( ) -> InvalidStatusCode {
519
522
InvalidStatusCode {
520
523
_priv : ( ) ,
521
524
}
0 commit comments