mod.rs
This file
Path
src/quantity/mod.rs
203 lines · 6.1 KiB · Rust
Last changed by
Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
History of this fileTake it
1use std::marker::PhantomData;23use uom::si;45pub use si::f64::{Information, Ratio, Time};6pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};7pub use si::ratio::ratio;8pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};9pub use uom::num_traits::Zero;1011pub use units::{InformationUnit, IsUnit, TimeUnit};1213pub mod statistics;14mod units;1516pub trait FormatQuantity {17 type Unit;1819 fn suitable_unit(&self) -> Self::Unit;2021 fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;22 fn format(&self, unit: Self::Unit) -> String;23 fn format_auto(&self) -> String;24 fn format_value(&self, unit: Self::Unit) -> String;25}2627impl FormatQuantity for Time {28 type Unit = TimeUnit;2930 fn suitable_unit(&self) -> TimeUnit {31 if *self < Time::new::<millisecond>(1.0) {32 TimeUnit::MicroSecond33 } else if *self < Time::new::<second>(1.0) {34 TimeUnit::MilliSecond35 } else {36 TimeUnit::Second37 }38 }3940 /// Format the time duration in the given unit with the given precision.41 fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String {42 u.format(*self, precision)43 }4445 /// Format the time duration in the given unit.46 fn format(&self, unit: TimeUnit) -> String {47 let value = self.format_with_precision(unit, unit.preferred_precision());48 format!("{} {}", value, unit.short_name())49 }5051 /// Format the given time duration. The unit will be determined automatically.52 fn format_auto(&self) -> String {53 self.format(self.suitable_unit())54 }5556 /// Like `format`, but without displaying the unit.57 fn format_value(&self, unit: TimeUnit) -> String {58 self.format_with_precision(unit, unit.preferred_precision())59 }60}6162pub const fn const_time_from_seconds(value: f64) -> Time {63 // Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html64 Time {65 dimension: PhantomData,66 units: PhantomData,67 value,68 }69}7071impl FormatQuantity for Information {72 type Unit = InformationUnit;7374 fn suitable_unit(&self) -> InformationUnit {75 if *self < Information::new::<kibibyte>(1.0) {76 InformationUnit::Byte77 } else if *self < Information::new::<mebibyte>(1.0) {78 InformationUnit::KibiByte79 } else if *self < Information::new::<gibibyte>(1.0) {80 InformationUnit::MebiByte81 } else {82 InformationUnit::GibiByte83 }84 }8586 /// Format the information in the given unit with the given precision.87 fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {88 u.format(*self, precision)89 }9091 /// Format the information in the given unit.92 fn format(&self, unit: InformationUnit) -> String {93 let value = self.format_with_precision(unit, unit.preferred_precision());94 format!("{} {}", value, unit.short_name())95 }9697 /// Format the given information. The unit will be determined automatically.98 fn format_auto(&self) -> String {99 self.format(self.suitable_unit())100 }101102 /// Like `format`, but without displaying the unit.103 fn format_value(&self, unit: InformationUnit) -> String {104 self.format_with_precision(unit, unit.preferred_precision())105 }106}107108#[test]109fn test_time() {110 let time = Time::new::<millisecond>(123.4);111 assert_eq!(time.get::<millisecond>(), 123.4);112113 let time_s = time.get::<second>();114 approx::assert_relative_eq!(time_s, 0.1234);115116 let time_us = time.get::<microsecond>();117 approx::assert_relative_eq!(time_us, 123400.0);118}119120#[test]121fn test_information() {122 use uom::si::information::pebibyte;123124 let information = Information::new::<kibibyte>(8.);125 assert_eq!(information.get::<byte>(), 8192.);126127 let information_kib = information.get::<kibibyte>();128 assert_eq!(information_kib, 8.);129130 let largest_exactly_representable = Information::new::<byte>(9_007_199_254_740_992.);131 assert_eq!(largest_exactly_representable.get::<pebibyte>(), 8.);132}133134#[test]135fn test_suiteable_unit_time() {136 assert_eq!(Time::new::<second>(1.3).suitable_unit(), TimeUnit::Second);137 assert_eq!(Time::new::<second>(1.0).suitable_unit(), TimeUnit::Second);138 assert_eq!(139 Time::new::<second>(0.999).suitable_unit(),140 TimeUnit::MilliSecond141 );142 assert_eq!(143 Time::new::<second>(0.0005).suitable_unit(),144 TimeUnit::MicroSecond145 );146 assert_eq!(147 Time::new::<second>(0.).suitable_unit(),148 TimeUnit::MicroSecond149 );150 assert_eq!(151 Time::new::<second>(1000.0).suitable_unit(),152 TimeUnit::Second153 );154}155156#[test]157fn test_suitable_unit_information() {158 assert_eq!(159 Information::new::<byte>(512.0).suitable_unit(),160 InformationUnit::Byte161 );162 assert_eq!(163 Information::new::<byte>(2048.0).suitable_unit(),164 InformationUnit::KibiByte165 );166 assert_eq!(167 Information::new::<byte>(2_097_152.0).suitable_unit(),168 InformationUnit::MebiByte169 );170 assert_eq!(171 Information::new::<byte>(2_147_483_648.0).suitable_unit(),172 InformationUnit::GibiByte173 );174}175176#[test]177fn test_format() {178 let time = Time::new::<millisecond>(123.4);179 assert_eq!(time.format(TimeUnit::Second), "0.123 s");180 assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");181 assert_eq!(time.format_auto(), "123.4 ms");182 assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");183184 let peak_memory_usage = Information::new::<kibibyte>(8.);185 assert_eq!(peak_memory_usage.format_auto(), "8.0 KiB");186 assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");187 assert_eq!(188 peak_memory_usage.format(InformationUnit::KibiByte),189 "8.0 KiB"190 );191}192193#[test]194fn test_format_duration_unit_with_unit() {195 let out = Time::new::<second>(1.3).format(TimeUnit::Second);196 assert_eq!("1.300 s", out);197198 let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);199 assert_eq!("1300.0 ms", out);200201 let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);202 assert_eq!("1300000.0 µs", out);203}