mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Extract typed quantity formatting
This commit is contained in:
1 parent
862625fb3d
commit
6f557e0494
6 files changed
+304
-129
No files matched your search
+135
-25
@@ -1,41 +1,146 @@
|
||||
//! This module contains common units.
|
||||
//! This module contains common units for time and information quantities.
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::quantity::{
|
||||
byte, gibibyte, hour, kibibyte, mebibyte, microsecond, millisecond, minute, second, tebibyte,
|
||||
Information, Time,
|
||||
};
|
||||
|
||||
pub trait IsUnit {
|
||||
type Quantity;
|
||||
|
||||
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Self::Quantity>>;
|
||||
|
||||
fn preferred_precision(&self) -> usize;
|
||||
|
||||
fn short_name(&self) -> &'static str {
|
||||
self.dispatch().short_name()
|
||||
}
|
||||
|
||||
fn format(&self, value: Self::Quantity, precision: usize) -> String {
|
||||
self.dispatch().format_value(value, precision)
|
||||
}
|
||||
}
|
||||
|
||||
/// Supported time units
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TimeUnit {
|
||||
Second,
|
||||
MilliSecond,
|
||||
MicroSecond,
|
||||
MilliSecond,
|
||||
Second,
|
||||
Minute,
|
||||
Hour,
|
||||
}
|
||||
|
||||
impl TimeUnit {
|
||||
/// The abbreviation of the TimeUnit.
|
||||
pub fn short_name(self) -> String {
|
||||
impl IsUnit for TimeUnit {
|
||||
type Quantity = Time;
|
||||
|
||||
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {
|
||||
match self {
|
||||
TimeUnit::Second => String::from("s"),
|
||||
TimeUnit::MilliSecond => String::from("ms"),
|
||||
TimeUnit::MicroSecond => String::from("µs"),
|
||||
TimeUnit::Minute => String::from("min"),
|
||||
TimeUnit::Hour => String::from("h"),
|
||||
TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),
|
||||
TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),
|
||||
TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),
|
||||
TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),
|
||||
TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value in seconds formatted for the TimeUnit.
|
||||
pub fn format(self, value: f64) -> String {
|
||||
fn preferred_precision(&self) -> usize {
|
||||
match self {
|
||||
TimeUnit::Second => format!("{value:.3}"),
|
||||
TimeUnit::MilliSecond => format!("{:.1}", value * 1e3),
|
||||
TimeUnit::MicroSecond => format!("{:.1}", value * 1e6),
|
||||
TimeUnit::Minute => format!("{:.1}", value / 60.0),
|
||||
TimeUnit::Hour => format!("{:.1}", value / 3600.0),
|
||||
TimeUnit::Second => 3,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Supported information units
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
||||
pub enum InformationUnit {
|
||||
Byte,
|
||||
KibiByte,
|
||||
MebiByte,
|
||||
GibiByte,
|
||||
TebiByte,
|
||||
}
|
||||
|
||||
impl IsUnit for InformationUnit {
|
||||
type Quantity = Information;
|
||||
|
||||
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {
|
||||
match self {
|
||||
InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),
|
||||
InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),
|
||||
InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),
|
||||
InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),
|
||||
InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn preferred_precision(&self) -> usize {
|
||||
match self {
|
||||
InformationUnit::Byte => 0,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait UnitImpl {
|
||||
type Quantity;
|
||||
|
||||
fn short_name(&self) -> &'static str;
|
||||
fn format_value(&self, value: Self::Quantity, precision: usize) -> String;
|
||||
}
|
||||
|
||||
struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {
|
||||
u: PhantomData<U>,
|
||||
}
|
||||
|
||||
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {
|
||||
fn new() -> Self {
|
||||
TimeUnitDispatcher { u: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {
|
||||
type Quantity = Time;
|
||||
|
||||
fn short_name(&self) -> &'static str {
|
||||
U::abbreviation()
|
||||
}
|
||||
|
||||
fn format_value(&self, value: Time, precision: usize) -> String {
|
||||
format!("{value:.precision$}", value = value.get::<U>())
|
||||
}
|
||||
}
|
||||
|
||||
struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {
|
||||
u: PhantomData<U>,
|
||||
}
|
||||
|
||||
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {
|
||||
fn new() -> Self {
|
||||
InformationUnitDispatcher { u: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl
|
||||
for InformationUnitDispatcher<U>
|
||||
{
|
||||
type Quantity = Information;
|
||||
|
||||
fn short_name(&self) -> &'static str {
|
||||
U::abbreviation()
|
||||
}
|
||||
|
||||
fn format_value(&self, value: Information, precision: usize) -> String {
|
||||
format!("{value:.precision$}", value = value.get::<U>())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unit_short_name() {
|
||||
fn test_time_unit_short_name() {
|
||||
assert_eq!("s", TimeUnit::Second.short_name());
|
||||
assert_eq!("ms", TimeUnit::MilliSecond.short_name());
|
||||
assert_eq!("µs", TimeUnit::MicroSecond.short_name());
|
||||
@@ -46,11 +151,16 @@ fn test_unit_short_name() {
|
||||
// Note - the values are rounded when formatted.
|
||||
#[test]
|
||||
fn test_unit_format() {
|
||||
let value: f64 = 123.456789;
|
||||
assert_eq!("123.457", TimeUnit::Second.format(value));
|
||||
assert_eq!("123456.8", TimeUnit::MilliSecond.format(value));
|
||||
use crate::quantity::FormatQuantity;
|
||||
|
||||
assert_eq!("1234.6", TimeUnit::MicroSecond.format(0.00123456));
|
||||
assert_eq!("2.1", TimeUnit::Minute.format(value));
|
||||
assert_eq!("0.0", TimeUnit::Hour.format(value));
|
||||
let value = Time::new::<second>(123.456789);
|
||||
assert_eq!("123.457", value.format_value(TimeUnit::Second));
|
||||
assert_eq!("123456.8", value.format_value(TimeUnit::MilliSecond));
|
||||
|
||||
assert_eq!(
|
||||
"1234.6",
|
||||
Time::new::<second>(0.00123456).format_value(TimeUnit::MicroSecond)
|
||||
);
|
||||
assert_eq!("2.1", value.format_value(TimeUnit::Minute));
|
||||
assert_eq!("0.0", value.format_value(TimeUnit::Hour));
|
||||
}
|
||||
Reference in new issue
Block a user