@@ -70,18 +70,17 @@ impl StatusCode {
7070 /// assert!(err.is_err());
7171 /// ```
7272 #[ inline]
73- pub fn from_u16 ( src : u16 ) -> Result < StatusCode , InvalidStatusCode > {
74- if !( 100 ..1000 ) . contains ( & src) {
75- return Err ( InvalidStatusCode :: new ( ) ) ;
73+ pub const fn from_u16 ( src : u16 ) -> Result < StatusCode , InvalidStatusCode > {
74+ if matches ! ( src, 100 ..=999 ) {
75+ if let Some ( code) = NonZeroU16 :: new ( src) {
76+ return Ok ( StatusCode ( code) ) ;
77+ }
7678 }
77-
78- NonZeroU16 :: new ( src)
79- . map ( StatusCode )
80- . ok_or_else ( InvalidStatusCode :: new)
79+ Err ( InvalidStatusCode :: new ( ) )
8180 }
8281
8382 /// Converts a `&[u8]` to a status code.
84- pub fn from_bytes ( src : & [ u8 ] ) -> Result < StatusCode , InvalidStatusCode > {
83+ pub const fn from_bytes ( src : & [ u8 ] ) -> Result < StatusCode , InvalidStatusCode > {
8584 if src. len ( ) != 3 {
8685 return Err ( InvalidStatusCode :: new ( ) ) ;
8786 }
@@ -95,9 +94,11 @@ impl StatusCode {
9594 }
9695
9796 let status = ( a * 100 ) + ( b * 10 ) + c;
98- NonZeroU16 :: new ( status)
99- . map ( StatusCode )
100- . ok_or_else ( InvalidStatusCode :: new)
97+ if let Some ( code) = NonZeroU16 :: new ( status) {
98+ Ok ( StatusCode ( code) )
99+ } else {
100+ Err ( InvalidStatusCode :: new ( ) )
101+ }
101102 }
102103
103104 /// Returns the `u16` corresponding to this `StatusCode`.
@@ -168,38 +169,47 @@ impl StatusCode {
168169 /// let status = http::StatusCode::OK;
169170 /// assert_eq!(status.canonical_reason(), Some("OK"));
170171 /// ```
171- pub fn canonical_reason ( & self ) -> Option < & ' static str > {
172+ pub const fn canonical_reason ( & self ) -> Option < & ' static str > {
172173 canonical_reason ( self . 0 . get ( ) )
173174 }
174175
175176 /// Check if status is within 100-199.
176177 #[ inline]
177- pub fn is_informational ( & self ) -> bool {
178- ( 100 .. 200 ) . contains ( & self . 0 . get ( ) )
178+ pub const fn is_informational ( & self ) -> bool {
179+ matches ! ( self . 0 . get( ) , 100 ..= 199 )
179180 }
180181
181182 /// Check if status is within 200-299.
182183 #[ inline]
183- pub fn is_success ( & self ) -> bool {
184- ( 200 .. 300 ) . contains ( & self . 0 . get ( ) )
184+ pub const fn is_success ( & self ) -> bool {
185+ matches ! ( self . 0 . get( ) , 200 ..= 299 )
185186 }
186187
187188 /// Check if status is within 300-399.
188189 #[ inline]
189- pub fn is_redirection ( & self ) -> bool {
190- ( 300 .. 400 ) . contains ( & self . 0 . get ( ) )
190+ pub const fn is_redirection ( & self ) -> bool {
191+ matches ! ( self . 0 . get( ) , 300 ..= 399 )
191192 }
192193
193194 /// Check if status is within 400-499.
194195 #[ inline]
195- pub fn is_client_error ( & self ) -> bool {
196- ( 400 .. 500 ) . contains ( & self . 0 . get ( ) )
196+ pub const fn is_client_error ( & self ) -> bool {
197+ matches ! ( self . 0 . get( ) , 400 ..= 499 )
197198 }
198199
199200 /// Check if status is within 500-599.
200201 #[ inline]
201- pub fn is_server_error ( & self ) -> bool {
202- ( 500 ..600 ) . contains ( & self . 0 . get ( ) )
202+ pub const fn is_server_error ( & self ) -> bool {
203+ matches ! ( self . 0 . get( ) , 500 ..=599 )
204+ }
205+
206+ /// Returns the "default value" for a type.
207+ ///
208+ /// This is implemented as an associated function in addition
209+ /// to the [`Default`] trait, to allow `const` usage.
210+ #[ inline]
211+ pub const fn default ( ) -> StatusCode {
212+ StatusCode :: OK
203213 }
204214}
205215
@@ -231,7 +241,7 @@ impl fmt::Display for StatusCode {
231241impl Default for StatusCode {
232242 #[ inline]
233243 fn default ( ) -> StatusCode {
234- StatusCode :: OK
244+ StatusCode :: default ( )
235245 }
236246}
237247
@@ -313,7 +323,7 @@ macro_rules! status_codes {
313323
314324 }
315325
316- fn canonical_reason( num: u16 ) -> Option <& ' static str > {
326+ const fn canonical_reason( num: u16 ) -> Option <& ' static str > {
317327 match num {
318328 $(
319329 $num => Some ( $phrase) ,
@@ -523,7 +533,7 @@ status_codes! {
523533}
524534
525535impl InvalidStatusCode {
526- fn new ( ) -> InvalidStatusCode {
536+ const fn new ( ) -> InvalidStatusCode {
527537 InvalidStatusCode { _priv : ( ) }
528538 }
529539}
0 commit comments