Skip to content

Commit

Permalink
Make StatusCode::as_str() return a &'static str explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Oct 28, 2022
1 parent 34a9d6b commit 8839d82
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/status.rs
Expand Up @@ -15,9 +15,9 @@
//! ```

use std::convert::TryFrom;
use std::num::NonZeroU16;
use std::error::Error;
use std::fmt;
use std::num::NonZeroU16;
use std::str::FromStr;

/// An HTTP status code (`status-code` in RFC 7230 et al.).
Expand Down Expand Up @@ -132,18 +132,22 @@ impl StatusCode {
/// assert_eq!(status.as_str(), "200");
/// ```
#[inline]
pub fn as_str(&self) -> &str {
pub fn as_str(&self) -> &'static str {
let offset = (self.0.get() - 100) as usize;
let offset = offset * 3;

// Invariant: self has checked range [100, 999] and CODE_DIGITS is
// ASCII-only, of length 900 * 3 = 2700 bytes

#[cfg(debug_assertions)]
{ &CODE_DIGITS[offset..offset+3] }
{
&CODE_DIGITS[offset..offset + 3]
}

#[cfg(not(debug_assertions))]
unsafe { CODE_DIGITS.get_unchecked(offset..offset+3) }
unsafe {
CODE_DIGITS.get_unchecked(offset..offset + 3)
}
}

/// Get the standardised `reason-phrase` for this status code.
Expand Down Expand Up @@ -516,9 +520,7 @@ status_codes! {

impl InvalidStatusCode {
fn new() -> InvalidStatusCode {
InvalidStatusCode {
_priv: (),
}
InvalidStatusCode { _priv: () }
}
}

Expand Down

0 comments on commit 8839d82

Please sign in to comment.