chore(stability): remove core feature gate

This commit is contained in:
Sean McArthur
2015-04-02 10:33:07 -07:00
parent b7d5920eb4
commit 5c2de29a81
10 changed files with 190 additions and 265 deletions

View File

@@ -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};
@@ -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"
}
@@ -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)),
@@ -71,7 +71,7 @@ impl Scheme for String {
}
fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
write!(f, "{}", self)
}
}

View File

@@ -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};
@@ -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,
@@ -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),

View File

@@ -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;
@@ -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
@@ -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())
}
}

View File

@@ -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.
@@ -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 {
@@ -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)
@@ -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
}
}
}
@@ -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> {
@@ -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::*;
@@ -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!(q(0.5), Quality(500));
}
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));
#[test]
#[should_panic]
fn test_quality_invalid() {
q(-1.0);
}
#[test]
#[should_panic]
fn test_quality_invalid2() {
q(2.0);
}
}