Skip to content

Commit 797b5dc

Browse files
feat: Make StatusCode methods const
1 parent 308104b commit 797b5dc

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/status.rs

Lines changed: 22 additions & 19 deletions
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> {
74-
if !(100..1000).contains(&src) {
73+
pub const fn from_u16(src: u16) -> Result<StatusCode, InvalidStatusCode> {
74+
if !(100 <= src && 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`
@@ -168,37 +171,37 @@ impl StatusCode {
168171
/// let status = http::StatusCode::OK;
169172
/// assert_eq!(status.canonical_reason(), Some("OK"));
170173
/// ```
171-
pub fn canonical_reason(&self) -> Option<&'static str> {
174+
pub const fn canonical_reason(&self) -> Option<&'static str> {
172175
canonical_reason(self.0.get())
173176
}
174177

175178
/// Check if status is within 100-199.
176179
#[inline]
177-
pub fn is_informational(&self) -> bool {
180+
pub const fn is_informational(&self) -> bool {
178181
200 > self.0.get() && self.0.get() >= 100
179182
}
180183

181184
/// Check if status is within 200-299.
182185
#[inline]
183-
pub fn is_success(&self) -> bool {
186+
pub const fn is_success(&self) -> bool {
184187
300 > self.0.get() && self.0.get() >= 200
185188
}
186189

187190
/// Check if status is within 300-399.
188191
#[inline]
189-
pub fn is_redirection(&self) -> bool {
192+
pub const fn is_redirection(&self) -> bool {
190193
400 > self.0.get() && self.0.get() >= 300
191194
}
192195

193196
/// Check if status is within 400-499.
194197
#[inline]
195-
pub fn is_client_error(&self) -> bool {
198+
pub const fn is_client_error(&self) -> bool {
196199
500 > self.0.get() && self.0.get() >= 400
197200
}
198201

199202
/// Check if status is within 500-599.
200203
#[inline]
201-
pub fn is_server_error(&self) -> bool {
204+
pub const fn is_server_error(&self) -> bool {
202205
600 > self.0.get() && self.0.get() >= 500
203206
}
204207
}
@@ -313,7 +316,7 @@ macro_rules! status_codes {
313316

314317
}
315318

316-
fn canonical_reason(num: u16) -> Option<&'static str> {
319+
const fn canonical_reason(num: u16) -> Option<&'static str> {
317320
match num {
318321
$(
319322
$num => Some($phrase),
@@ -519,7 +522,7 @@ status_codes! {
519522
}
520523

521524
impl InvalidStatusCode {
522-
fn new() -> InvalidStatusCode {
525+
const fn new() -> InvalidStatusCode {
523526
InvalidStatusCode { _priv: () }
524527
}
525528
}

0 commit comments

Comments
 (0)