Skip to content

Commit 9a94c5d

Browse files
feat: Make StatusCode methods const
1 parent 24bbec2 commit 9a94c5d

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/status.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,19 @@ impl StatusCode {
7070
/// assert!(err.is_err());
7171
/// ```
7272
#[inline]
73-
pub fn from_u16(src: u16) -> Result<StatusCode, InvalidStatusCode> {
73+
pub const fn from_u16(src: u16) -> Result<StatusCode, InvalidStatusCode> {
7474
if src < 100 || src >= 1000 {
7575
return Err(InvalidStatusCode::new());
7676
}
7777

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+
}
8182
}
8283

8384
/// 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> {
8586
if src.len() != 3 {
8687
return Err(InvalidStatusCode::new());
8788
}
@@ -95,9 +96,11 @@ impl StatusCode {
9596
}
9697

9798
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+
}
101104
}
102105

103106
/// Returns the `u16` corresponding to this `StatusCode`.
@@ -116,8 +119,8 @@ impl StatusCode {
116119
/// assert_eq!(status.as_u16(), 200);
117120
/// ```
118121
#[inline]
119-
pub fn as_u16(&self) -> u16 {
120-
(*self).into()
122+
pub const fn as_u16(&self) -> u16 {
123+
self.0.get()
121124
}
122125

123126
/// Returns a &str representation of the `StatusCode`
@@ -164,37 +167,37 @@ impl StatusCode {
164167
/// let status = http::StatusCode::OK;
165168
/// assert_eq!(status.canonical_reason(), Some("OK"));
166169
/// ```
167-
pub fn canonical_reason(&self) -> Option<&'static str> {
170+
pub const fn canonical_reason(&self) -> Option<&'static str> {
168171
canonical_reason(self.0.get())
169172
}
170173

171174
/// Check if status is within 100-199.
172175
#[inline]
173-
pub fn is_informational(&self) -> bool {
176+
pub const fn is_informational(&self) -> bool {
174177
200 > self.0.get() && self.0.get() >= 100
175178
}
176179

177180
/// Check if status is within 200-299.
178181
#[inline]
179-
pub fn is_success(&self) -> bool {
182+
pub const fn is_success(&self) -> bool {
180183
300 > self.0.get() && self.0.get() >= 200
181184
}
182185

183186
/// Check if status is within 300-399.
184187
#[inline]
185-
pub fn is_redirection(&self) -> bool {
188+
pub const fn is_redirection(&self) -> bool {
186189
400 > self.0.get() && self.0.get() >= 300
187190
}
188191

189192
/// Check if status is within 400-499.
190193
#[inline]
191-
pub fn is_client_error(&self) -> bool {
194+
pub const fn is_client_error(&self) -> bool {
192195
500 > self.0.get() && self.0.get() >= 400
193196
}
194197

195198
/// Check if status is within 500-599.
196199
#[inline]
197-
pub fn is_server_error(&self) -> bool {
200+
pub const fn is_server_error(&self) -> bool {
198201
600 > self.0.get() && self.0.get() >= 500
199202
}
200203
}
@@ -309,7 +312,7 @@ macro_rules! status_codes {
309312

310313
}
311314

312-
fn canonical_reason(num: u16) -> Option<&'static str> {
315+
const fn canonical_reason(num: u16) -> Option<&'static str> {
313316
match num {
314317
$(
315318
$num => Some($phrase),
@@ -515,7 +518,7 @@ status_codes! {
515518
}
516519

517520
impl InvalidStatusCode {
518-
fn new() -> InvalidStatusCode {
521+
const fn new() -> InvalidStatusCode {
519522
InvalidStatusCode {
520523
_priv: (),
521524
}

0 commit comments

Comments
 (0)