Skip to content

Commit d68773c

Browse files
committedMar 4, 2015
feat(headers): add q function to ease creating Quality values
1 parent 9e07708 commit d68773c

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed
 

Diff for: ‎src/header/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use unicase::UniCase;
2222
use self::cell::OptCell;
2323
use {http, HttpResult, HttpError};
2424

25-
pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem};
25+
pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem, q};
2626
pub use self::common::*;
2727

2828
mod cell;

Diff for: ‎src/header/shared/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use self::encoding::Encoding;
22
pub use self::entity::EntityTag;
3-
pub use self::quality_item::{Quality, QualityItem, qitem};
3+
pub use self::quality_item::{Quality, QualityItem, qitem, q};
44

55
mod encoding;
66
mod entity;

Diff for: ‎src/header/shared/quality_item.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use std::str;
1111

1212
/// Represents a quality used in quality values.
1313
///
14-
/// `Quality` should only be created using the `FromPrimitve` trait methods `from_f32` and
15-
/// `from_f64`, they take a value between 0.0 and 1.0. To create a quality with the value 1.0, the
16-
/// default you can use `let q: Quality = Default::default()`.
14+
/// Can be created with the `q` function.
1715
///
1816
/// # Implementation notes
17+
///
1918
/// The quality value is defined as a number between 0 and 1 with three decimal places. This means
2019
/// there are 1000 possible values. Since floating point numbers are not exact and the smallest
2120
/// floating point data type (`f32`) consumes four bytes, hyper uses an `u16` value to store the
@@ -149,6 +148,11 @@ pub fn qitem<T>(item: T) -> QualityItem<T> {
149148
QualityItem::new(item, Default::default())
150149
}
151150

151+
/// Convenience function to create a `Quality` fromt a float.
152+
pub fn q(f: f32) -> Quality {
153+
FromPrimitive::from_f32(f).expect("q value must be between 0.0 and 1.0")
154+
}
155+
152156
#[cfg(test)]
153157
mod tests {
154158
use std::num::FromPrimitive;

0 commit comments

Comments
 (0)