Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/status.rs
Original file line number Diff line number Diff line change
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