improve docs

This commit is contained in:
Rob Ede 2021-12-05 03:16:55 +00:00
parent 90b4f2a375
commit 228a512556
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 19 additions and 7 deletions

View File

@ -78,14 +78,21 @@ impl fmt::Display for Quality {
// This implementation avoids string allocation for removing trailing zeroes. // This implementation avoids string allocation for removing trailing zeroes.
// In benchmarks it is twice as fast as approach using something like // 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 { if x < 10 {
// x in is range 19
f.write_str("00")?; 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) itoa::fmt(f, x)
} else if x < 100 { } else if x < 100 {
// x in is range 1099
f.write_str("0")?; f.write_str("0")?;
if x % 10 == 0 { if x % 10 == 0 {
// trailing 0, divide by 10 and write // trailing 0, divide by 10 and write
itoa::fmt(f, x / 10) itoa::fmt(f, x / 10)
@ -93,7 +100,7 @@ impl fmt::Display for Quality {
itoa::fmt(f, x) itoa::fmt(f, x)
} }
} else { } else {
// x is in range 101999 // x is in range 100999
if x % 100 == 0 { if x % 100 == 0 {
// two trailing 0s, divide by 100 and write // 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.22).to_string(), "0.22");
assert_eq!(q(0.123).to_string(), "0.123"); assert_eq!(q(0.123).to_string(), "0.123");
assert_eq!(q(0.999).to_string(), "0.999"); 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] #[test]

View File

@ -48,12 +48,12 @@ impl<T> QualityItem<T> {
QualityItem { item, quality } 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 { pub fn max(item: T) -> Self {
Self::new(item, Quality::MAX) 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 { pub fn min(item: T) -> Self {
Self::new(item, Quality::MIN) Self::new(item, Quality::MIN)
} }
@ -74,7 +74,6 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
Quality::MAX => Ok(()), Quality::MAX => Ok(()),
Quality::MIN => f.write_str("; q=0"), Quality::MIN => f.write_str("; q=0"),
q => write!(f, "; q={}", q), q => write!(f, "; q={}", q),
} }
} }
@ -88,7 +87,7 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
return Err(ParseError::Header); 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 raw_item = q_item_str;
let mut quality = Quality::MAX; let mut quality = Quality::MAX;