From 228a512556e72c967ab0b2f735921966e99acbf3 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 5 Dec 2021 03:16:55 +0000 Subject: [PATCH] improve docs --- actix-http/src/header/shared/quality.rs | 19 ++++++++++++++++--- actix-http/src/header/shared/quality_item.rs | 7 +++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/actix-http/src/header/shared/quality.rs b/actix-http/src/header/shared/quality.rs index 80c823f1b..5321c754d 100644 --- a/actix-http/src/header/shared/quality.rs +++ b/actix-http/src/header/shared/quality.rs @@ -78,14 +78,21 @@ impl fmt::Display for Quality { // This implementation avoids string allocation for removing trailing zeroes. // In benchmarks it is twice as fast as approach using something like - // `format!("{}").trim_end_matches('0')` for non-fast path quality values. + // `format!("{}").trim_end_matches('0')` for non-fast-path quality values. if x < 10 { + // x in is range 1–9 + f.write_str("00")?; - // 0 is handled so it's not possible to have a trailing 0, we can just return + + // 0 is already handled so it's not possible to have a trailing 0 in this range + // we can just write the integer itoa::fmt(f, x) } else if x < 100 { + // x in is range 10–99 + f.write_str("0")?; + if x % 10 == 0 { // trailing 0, divide by 10 and write itoa::fmt(f, x / 10) @@ -93,7 +100,7 @@ impl fmt::Display for Quality { itoa::fmt(f, x) } } else { - // x is in range 101–999 + // x is in range 100–999 if x % 100 == 0 { // two trailing 0s, divide by 100 and write @@ -179,6 +186,12 @@ mod tests { assert_eq!(q(0.22).to_string(), "0.22"); assert_eq!(q(0.123).to_string(), "0.123"); assert_eq!(q(0.999).to_string(), "0.999"); + + for x in 0..=1000 { + // if trailing zeroes are handled correctly, we would not expect the serialized length + // to ever exceed "0." + 3 decimal places = 5 in length + assert!(q(x as f32 / 1000.0).to_string().len() <= 5); + } } #[test] diff --git a/actix-http/src/header/shared/quality_item.rs b/actix-http/src/header/shared/quality_item.rs index ac1e2b80b..9354915ad 100644 --- a/actix-http/src/header/shared/quality_item.rs +++ b/actix-http/src/header/shared/quality_item.rs @@ -48,12 +48,12 @@ impl QualityItem { QualityItem { item, quality } } - /// Constructs a new `QualityItem` with from an item, using the maximum q-value. + /// Constructs a new `QualityItem` from an item, using the maximum q-value. pub fn max(item: T) -> Self { Self::new(item, Quality::MAX) } - /// Constructs a new `QualityItem` with from an item, using the minimum q-value. + /// Constructs a new `QualityItem` from an item, using the minimum q-value. pub fn min(item: T) -> Self { Self::new(item, Quality::MIN) } @@ -74,7 +74,6 @@ impl fmt::Display for QualityItem { Quality::MAX => Ok(()), Quality::MIN => f.write_str("; q=0"), - q => write!(f, "; q={}", q), } } @@ -88,7 +87,7 @@ impl str::FromStr for QualityItem { return Err(ParseError::Header); } - // set defaults used if quality-item parsing fails, i.e., item has no q-factor + // set defaults used if quality-item parsing fails, i.e., item has no q attribute let mut raw_item = q_item_str; let mut quality = Quality::MAX;