Skip to content

Commit 9f86d52

Browse files
feat: make StatusCode::from_u16 const (#761)
Co-authored-by: Sean McArthur <[email protected]>
1 parent d9af498 commit 9f86d52

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/status.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ 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 let 100..=999 = src {
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.
@@ -523,7 +522,7 @@ status_codes! {
523522
}
524523

525524
impl InvalidStatusCode {
526-
fn new() -> InvalidStatusCode {
525+
const fn new() -> InvalidStatusCode {
527526
InvalidStatusCode { _priv: () }
528527
}
529528
}

0 commit comments

Comments
 (0)