change Quality::MIN and add Quality::ZERO

This commit is contained in:
Rob Ede 2022-01-01 08:55:24 +00:00
parent 762971efb5
commit 8178844e7d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 16 additions and 3 deletions

View File

@ -4,6 +4,12 @@
### Added ### Added
- `impl Eq` for `header::ContentEncoding`. [#2501] - `impl Eq` for `header::ContentEncoding`. [#2501]
- `impl Copy` for `QualityItem` where `T: Copy`. [#2501] - `impl Copy` for `QualityItem` where `T: Copy`. [#2501]
- `Quality::ZERO` equivalent to `q=0`. [#2501]
- `QualityItem::zero` that uses `Quality::ZERO`. [#2501]
### Changed
- `Quality::MIN` is now the smallest non-zero value. [#2501]
- `QualityItem::min` has different semantics due to the `QualityItem::MIN` change. [#2501]
[#2501]: https://github.com/actix/actix-web/pull/2501 [#2501]: https://github.com/actix/actix-web/pull/2501

View File

@ -38,8 +38,11 @@ impl Quality {
/// The maximum quality value, equivalent to `q=1.0`. /// The maximum quality value, equivalent to `q=1.0`.
pub const MAX: Quality = Quality(MAX_QUALITY_INT); pub const MAX: Quality = Quality(MAX_QUALITY_INT);
/// The minimum quality value, equivalent to `q=0.0`. /// The minimum, non-zero quality value, equivalent to `q=0.001`.
pub const MIN: Quality = Quality(0); pub const MIN: Quality = Quality(1);
/// The zero quality value, equivalent to `q=0.0`.
pub const ZERO: Quality = Quality(0);
/// Converts a float in the range 0.01.0 to a `Quality`. /// Converts a float in the range 0.01.0 to a `Quality`.
/// ///
@ -51,7 +54,7 @@ impl Quality {
// Check that `value` is within range should be done before calling this method. // Check that `value` is within range should be done before calling this method.
// Just in case, this debug_assert should catch if we were forgetful. // Just in case, this debug_assert should catch if we were forgetful.
debug_assert!( debug_assert!(
(0.0f32..=1.0f32).contains(&value), (0.0..=MAX_QUALITY_FLOAT).contains(&value),
"q value must be between 0.0 and 1.0" "q value must be between 0.0 and 1.0"
); );
@ -185,6 +188,10 @@ mod tests {
#[test] #[test]
fn display_output() { fn display_output() {
assert_eq!(Quality::ZERO.to_string(), "0");
assert_eq!(Quality::MIN.to_string(), "0.001");
assert_eq!(Quality::MAX.to_string(), "1");
assert_eq!(q(0.0).to_string(), "0"); assert_eq!(q(0.0).to_string(), "0");
assert_eq!(q(1.0).to_string(), "1"); assert_eq!(q(1.0).to_string(), "1");
assert_eq!(q(0.001).to_string(), "0.001"); assert_eq!(q(0.001).to_string(), "0.001");