Skip to content
This repository has been archived by the owner on Aug 23, 2018. It is now read-only.

Commit

Permalink
Rust update for status code.
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-morgan committed Jul 17, 2015
1 parent 5d897d7 commit 8ea20dc
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions src/httpcommon/status.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! HTTP status codes.
use std::fmt;
use std::mem::transmute;
use std::num::{FromPrimitive, ToPrimitive};
use std::mem;
use std::cmp::Ordering;

pub use self::StatusCode::*;
Expand Down Expand Up @@ -2196,20 +2195,13 @@ impl Clone for StatusCode {

// Of the other common derivable traits, I didn’t measure them, but I guess they would be slow too.

impl FromPrimitive for StatusCode {
fn from_i64(n: i64) -> Option<StatusCode> {
if n < 100 || n > 599 {
None
} else {
Some(unsafe { transmute::<u16, StatusCode>(n as u16) })
}
}

fn from_u64(n: u64) -> Option<StatusCode> {
if n < 100 || n > 599 {
None
impl StatusCode {
/// Convert a `u16` to a `StatusCode` if it is in the legal range (100–599).
pub fn from_u16(n: u16) -> Result<StatusCode, ()> {
if n >= 100 && n <= 599 {
Result::Ok(unsafe { mem::transmute(n) })
} else {
Some(unsafe { transmute::<u16, StatusCode>(n as u16) })
Err(())
}
}
}
Expand All @@ -2234,16 +2226,6 @@ impl Ord for StatusCode {
}
}

impl ToPrimitive for StatusCode {
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
}

fn to_u64(&self) -> Option<u64> {
Some(*self as u64)
}
}

/// The class of an HTTP `status-code`.
///
/// [RFC 7231, section 6 (Response Status Codes)](https://tools.ietf.org/html/rfc7231#section-6):
Expand Down Expand Up @@ -2332,16 +2314,6 @@ impl StatusClass {
/// ```
#[inline]
pub fn default_code(&self) -> StatusCode {
unsafe { transmute::<StatusClass, StatusCode>(*self) }
}
}

impl ToPrimitive for StatusClass {
fn to_i64(&self) -> Option<i64> {
Some(*self as i64)
}

fn to_u64(&self) -> Option<u64> {
Some(*self as u64)
unsafe { mem::transmute(*self) }
}
}

0 comments on commit 8ea20dc

Please sign in to comment.