Skip to content
Myr

units.rs

This file

Path

src/quantity/units.rs

166 lines · 4.7 KiB · Rust

Last changed by

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

History of this file

Take it

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

line 1why, one step back
1//! This module contains common units for time and information quantities.

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 2why, one step back
2

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 3–25why, one step back
3use std::marker::PhantomData;
45use crate::quantity::{6 byte, gibibyte, hour, kibibyte, mebibyte, microsecond, millisecond, minute, second, tebibyte,7 Information, Time,8};910pub trait IsUnit {11 type Quantity;1213 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Self::Quantity>>;1415 fn preferred_precision(&self) -> usize;1617 fn short_name(&self) -> &'static str {18 self.dispatch().short_name()19 }2021 fn format(&self, value: Self::Quantity, precision: usize) -> String {22 self.dispatch().format_value(value, precision)23 }24}25

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

lines 26–29why, one step back
26/// Supported time units
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]28pub enum TimeUnit {29 MicroSecond,

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 30–31why, one step back
30 MilliSecond,
31 Second,

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

lines 32–35why, one step back
32 Minute,
33 Hour,34}35

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 36–39why, one step back
36impl IsUnit for TimeUnit {
37 type Quantity = Time;3839 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 40why, one step back
40 match self {

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 41–45why, one step back
41 TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),
42 TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),43 TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),44 TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),45 TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

lines 46–48why, one step back
46 }
47 }48

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

line 49why, one step back
49 fn preferred_precision(&self) -> usize {

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 50why, one step back
50 match self {

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 51–52why, one step back
51 TimeUnit::Second => 3,
52 _ => 1,

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

lines 53–56why, one step back
53 }
54 }55}56

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 57–141why, one step back
57/// Supported information units
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]5960pub enum InformationUnit {61 Byte,62 KibiByte,63 MebiByte,64 GibiByte,65 TebiByte,66}6768impl IsUnit for InformationUnit {69 type Quantity = Information;7071 fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {72 match self {73 InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),74 InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),75 InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),76 InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),77 InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),78 }79 }8081 fn preferred_precision(&self) -> usize {82 match self {83 InformationUnit::Byte => 0,84 _ => 1,85 }86 }87}8889pub trait UnitImpl {90 type Quantity;9192 fn short_name(&self) -> &'static str;93 fn format_value(&self, value: Self::Quantity, precision: usize) -> String;94}9596struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {97 u: PhantomData<U>,98}99100impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {101 fn new() -> Self {102 TimeUnitDispatcher { u: PhantomData }103 }104}105106impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {107 type Quantity = Time;108109 fn short_name(&self) -> &'static str {110 U::abbreviation()111 }112113 fn format_value(&self, value: Time, precision: usize) -> String {114 format!("{value:.precision$}", value = value.get::<U>())115 }116}117118struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {119 u: PhantomData<U>,120}121122impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {123 fn new() -> Self {124 InformationUnitDispatcher { u: PhantomData }125 }126}127128impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl129 for InformationUnitDispatcher<U>130{131 type Quantity = Information;132133 fn short_name(&self) -> &'static str {134 U::abbreviation()135 }136137 fn format_value(&self, value: Information, precision: usize) -> String {138 format!("{value:.precision$}", value = value.get::<U>())139 }140}141

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 142why, one step back
142#[test]

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

line 143why, one step back
143fn test_time_unit_short_name() {

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

lines 144–153why, one step back
144 assert_eq!("s", TimeUnit::Second.short_name());
145 assert_eq!("ms", TimeUnit::MilliSecond.short_name());146 assert_eq!("µs", TimeUnit::MicroSecond.short_name());147 assert_eq!("min", TimeUnit::Minute.short_name());148 assert_eq!("h", TimeUnit::Hour.short_name());149}150151// Note - the values are rounded when formatted.152#[test]153fn test_unit_format() {

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

line 154why, one step back
154 use crate::quantity::FormatQuantity;

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 155why, one step back
155

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

lines 156–165why, one step back
156 let value = Time::new::<second>(123.456789);
157 assert_eq!("123.457", value.format_value(TimeUnit::Second));158 assert_eq!("123456.8", value.format_value(TimeUnit::MilliSecond));159160 assert_eq!(161 "1234.6",162 Time::new::<second>(0.00123456).format_value(TimeUnit::MicroSecond)163 );164 assert_eq!("2.1", value.format_value(TimeUnit::Minute));165 assert_eq!("0.0", value.format_value(TimeUnit::Hour));

Merge pull request #962 from sharkdp/codex/quantity-module-layoutDavid Peterbefore adoptionecf8a24

line 166why, one step back
166}