diff --git a/src/header/mod.rs b/src/header/mod.rs index f19c6e6205..9b8338cae9 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -22,7 +22,7 @@ use unicase::UniCase; use self::cell::OptCell; use {http, HttpResult, HttpError}; -pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem}; +pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem, q}; pub use self::common::*; mod cell; diff --git a/src/header/shared/mod.rs b/src/header/shared/mod.rs index dd81208abe..83f1c1c525 100644 --- a/src/header/shared/mod.rs +++ b/src/header/shared/mod.rs @@ -1,6 +1,6 @@ pub use self::encoding::Encoding; pub use self::entity::EntityTag; -pub use self::quality_item::{Quality, QualityItem, qitem}; +pub use self::quality_item::{Quality, QualityItem, qitem, q}; mod encoding; mod entity; diff --git a/src/header/shared/quality_item.rs b/src/header/shared/quality_item.rs index 814668d4d9..fd191d1269 100644 --- a/src/header/shared/quality_item.rs +++ b/src/header/shared/quality_item.rs @@ -11,11 +11,10 @@ use std::str; /// Represents a quality used in quality values. /// -/// `Quality` should only be created using the `FromPrimitve` trait methods `from_f32` and -/// `from_f64`, they take a value between 0.0 and 1.0. To create a quality with the value 1.0, the -/// default you can use `let q: Quality = Default::default()`. +/// Can be created with the `q` function. /// /// # Implementation notes +/// /// The quality value is defined as a number between 0 and 1 with three decimal places. This means /// there are 1000 possible values. Since floating point numbers are not exact and the smallest /// floating point data type (`f32`) consumes four bytes, hyper uses an `u16` value to store the @@ -149,6 +148,11 @@ pub fn qitem(item: T) -> QualityItem { QualityItem::new(item, Default::default()) } +/// 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") +} + #[cfg(test)] mod tests { use std::num::FromPrimitive;