Skip to content

Commit 5ba5778

Browse files
committed
improve StatusCode as_str
1 parent 1d9ddb1 commit 5ba5778

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/status.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,14 @@ impl StatusCode {
131131
/// ```
132132
#[inline]
133133
pub fn as_str(&self) -> &str {
134-
CODES_AS_STR[(self.0.get() - 100) as usize]
134+
let offset = (self.0.get() - 100) as usize;
135+
let offset = offset * 3;
136+
137+
unsafe {
138+
CODES_AS_STR
139+
.get_unchecked(offset..)
140+
.get_unchecked(..3)
141+
}
135142
}
136143

137144
/// Get the standardised `reason-phrase` for this status code.
@@ -528,7 +535,11 @@ impl Error for InvalidStatusCode {}
528535

529536
macro_rules! status_code_strs {
530537
($($num:expr,)+) => {
531-
const CODES_AS_STR: [&'static str; 900] = [ $( stringify!($num), )+ ];
538+
const CODES_AS_STR: &'static str = concat!(
539+
$(
540+
stringify!($num) ,
541+
)+
542+
);
532543
}
533544
}
534545

0 commit comments

Comments
 (0)