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
Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 1–2why, one step back1use std::marker::PhantomData;2Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 3–8why, one step back3use 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};Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 9–10why, one step back9pub use uom::num_traits::Zero;10Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
line 11why, one step back11pub use units::{InformationUnit, IsUnit, TimeUnit};Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 12–15why, one step back1213pub mod statistics;14mod units;15Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 16–61why, one step back16pub 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}61Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 62–70why, one step back62pub 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}70Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 71–72why, one step back71impl FormatQuantity for Information {72 type Unit = InformationUnit;Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 73why, one step back73Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 74–84why, one step back74 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 }Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 85why, one step back85Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 86–89why, one step back86 /// 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 }Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 90why, one step back90Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 91–94why, one step back91 /// 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())Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 95–96why, one step back95 }96Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 97–100why, one step back97 /// Format the given information. The unit will be determined automatically.98 fn format_auto(&self) -> String {99 self.format(self.suitable_unit())100 }Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 101why, one step back101Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 102–107why, one step back102 /// 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}107Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 108–134why, one step back108#[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]Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 135–154why, one step back135fn 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}Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 155why, one step back155Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 156–174why, one step back156#[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}Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 175why, one step back175Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 176–182why, one step back176#[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");Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 183why, one step back183Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 184–190why, one step back184 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 );Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
lines 191–194why, one step back191}192193#[test]194fn test_format_duration_unit_with_unit() {Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 195–196why, one step back195 let out = Time::new::<second>(1.3).format(TimeUnit::Second);196 assert_eq!("1.300 s", out);Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 197why, one step back197Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 198–199why, one step back198 let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);199 assert_eq!("1300.0 ms", out);Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 200why, one step back200Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010
lines 201–202why, one step back201 let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);202 assert_eq!("1300000.0 µs", out);Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24
line 203why, one step back203}