Skip to content

Commit

Permalink
Merge pull request #425 from hyperium/unfeat-core
Browse files Browse the repository at this point in the history
chore(stability): remove core feature gate
  • Loading branch information
seanmonstar committed Apr 3, 2015
2 parents b7d5920 + 5c2de29 commit 8f61dd4
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 266 deletions.
7 changes: 1 addition & 6 deletions src/client/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Client Responses
use std::io::{self, Read};
use std::num::FromPrimitive;
use std::marker::PhantomData;

use buffer::BufReader;
Expand All @@ -13,7 +12,6 @@ use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
use status;
use version;
use HttpResult;
use HttpError::HttpStatusError;

/// A response for a client request to a remote server.
pub struct Response<S = HttpStream> {
Expand All @@ -39,10 +37,7 @@ impl Response {
let raw_status = head.subject;
let headers = head.headers;

let status = match FromPrimitive::from_u16(raw_status.0) {
Some(status) => status,
None => return Err(HttpStatusError)
};
let status = status::StatusCode::from_u16(raw_status.0);
debug!("version={:?}, status={:?}", head.version, status);
debug!("headers={:?}", headers);

Expand Down
8 changes: 4 additions & 4 deletions src/header/common/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::any::Any;
use std::fmt;
use std::str::{FromStr, from_utf8};
use std::ops::{Deref, DerefMut};
use std::marker::Reflect;
use serialize::base64::{ToBase64, FromBase64, Standard, Config, Newline};
use header::{Header, HeaderFormat};

Expand All @@ -23,7 +23,7 @@ impl<S: Scheme> DerefMut for Authorization<S> {
}
}

impl<S: Scheme + Reflect + 'static> Header for Authorization<S> where <S as FromStr>::Err: 'static {
impl<S: Scheme + Any> Header for Authorization<S> where <S as FromStr>::Err: 'static {
fn header_name() -> &'static str {
"Authorization"
}
Expand All @@ -44,7 +44,7 @@ impl<S: Scheme + Reflect + 'static> Header for Authorization<S> where <S as From
}
}

impl<S: Scheme + Reflect + 'static> HeaderFormat for Authorization<S> where <S as FromStr>::Err: 'static {
impl<S: Scheme + Any> HeaderFormat for Authorization<S> where <S as FromStr>::Err: 'static {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match Scheme::scheme(None::<S>) {
Some(scheme) => try!(write!(fmt, "{} ", scheme)),
Expand All @@ -71,7 +71,7 @@ impl Scheme for String {
}

fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
write!(f, "{}", self)
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/header/internals/item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::any::Any;
use std::any::TypeId;
use std::fmt;
use std::str::from_utf8;

use typeable::Typeable;

use super::cell::{OptCell, PtrMapCell};
use header::{Header, HeaderFormat};

Expand All @@ -24,7 +27,7 @@ impl Item {
#[inline]
pub fn new_typed(ty: Box<HeaderFormat + Send + Sync>) -> Item {
let map = PtrMapCell::new();
unsafe { map.insert((&*ty).get_type_id(), ty); }
unsafe { map.insert((*ty).get_type(), ty); }
Item {
raw: OptCell::new(None),
typed: map,
Expand All @@ -51,7 +54,7 @@ impl Item {
&raw[..]
}

pub fn typed<H: Header + HeaderFormat>(&self) -> Option<&H> {
pub fn typed<H: Header + HeaderFormat + Any>(&self) -> Option<&H> {
let tid = TypeId::of::<H>();
match self.typed.get(tid) {
Some(val) => Some(val),
Expand Down
9 changes: 5 additions & 4 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::iter::{FromIterator, IntoIterator};
use std::{mem, fmt};

use {httparse, traitobject};
use typeable::Typeable;
use unicase::UniCase;

use self::internals::Item;
Expand Down Expand Up @@ -50,7 +51,7 @@ pub trait Header: Clone + Any + Send + Sync {
/// A trait for any object that will represent a header field and value.
///
/// This trait represents the formatting of a Header for output to a TcpStream.
pub trait HeaderFormat: fmt::Debug + HeaderClone + Any + Send + Sync {
pub trait HeaderFormat: fmt::Debug + HeaderClone + Any + Typeable + Send + Sync {
/// Format a header to be output into a TcpStream.
///
/// This method is not allowed to introduce an Err not produced
Expand All @@ -61,12 +62,12 @@ pub trait HeaderFormat: fmt::Debug + HeaderClone + Any + Send + Sync {

#[doc(hidden)]
pub trait HeaderClone {
fn clone_box(&self) -> Box<HeaderFormat + Sync + Send>;
fn clone_box(&self) -> Box<HeaderFormat + Send + Sync>;
}

impl<T: HeaderFormat + Send + Sync + Clone> HeaderClone for T {
impl<T: HeaderFormat + Clone> HeaderClone for T {
#[inline]
fn clone_box(&self) -> Box<HeaderFormat + Sync + Send> {
fn clone_box(&self) -> Box<HeaderFormat + Send + Sync> {
Box::new(self.clone())
}
}
Expand Down
92 changes: 36 additions & 56 deletions src/header/shared/quality_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use std::cmp;
use std::default::Default;
use std::fmt;
use std::num::{FromPrimitive, ToPrimitive};
use std::str;

/// Represents a quality used in quality values.
Expand All @@ -20,7 +19,7 @@ use std::str;
/// floating point data type (`f32`) consumes four bytes, hyper uses an `u16` value to store the
/// quality internally. For performance reasons you may set quality directly to a value between
/// 0 and 1000 e.g. `Quality(532)` matches the quality `q=0.532`.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Quality(pub u16);

impl fmt::Display for Quality {
Expand All @@ -33,43 +32,6 @@ impl fmt::Display for Quality {
}
}

impl FromPrimitive for Quality {
fn from_i64(n: i64) -> Option<Quality> {
match n >= 0 {
true => FromPrimitive::from_u64(n as u64),
false => None,
}
}

fn from_u64(n: u64) -> Option<Quality> {
match n <= 1000 {
true => Some(Quality(n as u16)),
false => None,
}
}

fn from_f64(n: f64) -> Option<Quality> {
match n >= 0f64 && n <= 1f64 {
true => Some(Quality((n * 1000f64) as u16)),
false => None,
}
}
}

impl ToPrimitive for Quality {
fn to_i64(&self) -> Option<i64> {
Some(self.0 as i64)
}

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

fn to_f64(&self) -> Option<f64> {
Some((self.0 as f64) / 1000f64)
}
}

impl Default for Quality {
fn default() -> Quality {
Quality(1000)
Expand All @@ -91,7 +53,10 @@ impl<T> QualityItem<T> {
/// The item can be of any type.
/// The quality should be a value in the range [0, 1].
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
QualityItem{item: item, quality: quality}
QualityItem {
item: item,
quality: quality
}
}
}

Expand Down Expand Up @@ -136,12 +101,21 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
}
}
match raw_item.parse::<T>() {
Ok(item) => Ok(QualityItem::new(item, FromPrimitive::from_f32(quality).unwrap())),
// we already checked above that the quality is within range
Ok(item) => Ok(QualityItem::new(item, from_f32(quality))),
Err(_) => return Err(()),
}
}
}

fn from_f32(f: f32) -> Quality {
// this function is only used internally. A check that `f` is within range
// should be done before calling this method. Just in case, this
// debug_assert should catch if we were forgetful
debug_assert!(f >= 0f32 && f <= 1f32, "q value must be between 0.0 and 1.0");
Quality((f * 1000f32) as u16)
}

/// Convinience function to wrap a value in a `QualityItem`
/// Sets `q` to the default 1.0
pub fn qitem<T>(item: T) -> QualityItem<T> {
Expand All @@ -150,13 +124,12 @@ pub fn qitem<T>(item: T) -> QualityItem<T> {

/// Convenience function to create a `Quality` fromt a float.
pub fn q(f: f32) -> Quality {
FromPrimitive::from_f32(f).expect("q value must be between 0.0 and 1.0")
assert!(f >= 0f32 && f <= 1f32, "q value must be between 0.0 and 1.0");
from_f32(f)
}

#[cfg(test)]
mod tests {
use std::num::FromPrimitive;

use super::*;
use super::super::encoding::*;

Expand Down Expand Up @@ -206,25 +179,32 @@ mod tests {
assert_eq!(x, Err(()));
}
#[test]
fn test_quality_item_from_str6() {
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=2".parse();
assert_eq!(x, Err(()));
}
#[test]
fn test_quality_item_ordering() {
let x: QualityItem<Encoding> = "gzip; q=0.5".parse().ok().unwrap();
let y: QualityItem<Encoding> = "gzip; q=0.273".parse().ok().unwrap();
let comparision_result: bool = x.gt(&y);
assert_eq!(comparision_result, true)
}

#[test]
fn test_quality() {
assert_eq!(Some(Quality(421)), FromPrimitive::from_f64(0.421f64));
assert_eq!(Some(Quality(421)), FromPrimitive::from_f32(0.421f32));
assert_eq!(Some(Quality(421)), FromPrimitive::from_i16(421i16));
assert_eq!(Some(Quality(421)), FromPrimitive::from_i32(421i32));
assert_eq!(Some(Quality(421)), FromPrimitive::from_i64(421i64));
assert_eq!(Some(Quality(421)), FromPrimitive::from_u16(421u16));
assert_eq!(Some(Quality(421)), FromPrimitive::from_u32(421u32));
assert_eq!(Some(Quality(421)), FromPrimitive::from_u64(421u64));

assert_eq!(None::<Quality>, FromPrimitive::from_i16(-5i16));
assert_eq!(None::<Quality>, FromPrimitive::from_i32(5000i32));
assert_eq!(None::<Quality>, FromPrimitive::from_f32(2.5f32));
assert_eq!(q(0.5), Quality(500));
}

#[test]
#[should_panic]
fn test_quality_invalid() {
q(-1.0);
}

#[test]
#[should_panic]
fn test_quality_invalid2() {
q(2.0);
}
}
8 changes: 2 additions & 6 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::borrow::{Cow, ToOwned};
use std::cmp::min;
use std::io::{self, Read, Write, BufRead};
use std::num::FromPrimitive;

use httparse;

Expand Down Expand Up @@ -378,11 +377,8 @@ impl<'a> TryParse for httparse::Response<'a> {
Ok(match try!(res.parse(buf)) {
httparse::Status::Complete(len) => {
let code = res.code.unwrap();
let reason = match <StatusCode as FromPrimitive>::from_u16(code) {
Some(status) => match status.canonical_reason() {
Some(reason) => Cow::Borrowed(reason),
None => Cow::Owned(res.reason.unwrap().to_owned())
},
let reason = match StatusCode::from_u16(code).canonical_reason() {
Some(reason) => Cow::Borrowed(reason),
None => Cow::Owned(res.reason.unwrap().to_owned())
};
httparse::Status::Complete((Incoming {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![doc(html_root_url = "https://hyperium.github.io/hyper/hyper/index.html")]
#![feature(core)]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(test, feature(test))]
Expand Down
5 changes: 2 additions & 3 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener};
use std::mem;
use std::path::Path;
use std::sync::Arc;
use std::marker::Reflect;

use openssl::ssl::{Ssl, SslStream, SslContext};
use openssl::ssl::SslVerifyMode::SslVerifyNone;
Expand Down Expand Up @@ -135,7 +134,7 @@ impl NetworkStream + Send {
/// If the underlying type is T, get a mutable reference to the contained
/// data.
#[inline]
pub fn downcast_mut<T: Reflect + 'static>(&mut self) -> Option<&mut T> {
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
Some(unsafe { self.downcast_mut_unchecked() })
} else {
Expand All @@ -144,7 +143,7 @@ impl NetworkStream + Send {
}

/// If the underlying type is T, extract it.
pub fn downcast<T: Reflect + 'static>(self: Box<NetworkStream + Send>)
pub fn downcast<T: Any>(self: Box<NetworkStream + Send>)
-> Result<Box<T>, Box<NetworkStream + Send>> {
if self.is::<T>() {
Ok(unsafe { self.downcast_unchecked() })
Expand Down
Loading

0 comments on commit 8f61dd4

Please sign in to comment.