mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Move a few modules to util
This commit is contained in:
1 parent
999e7c4566
commit
5c634c9f2f
22 files changed
+22
-21
No files matched your search
@@ -0,0 +1,30 @@
|
||||
use std::iter::Iterator;
|
||||
|
||||
/// A max function for f64's without NaNs
|
||||
pub fn max(vals: &[f64]) -> f64 {
|
||||
*vals
|
||||
.iter()
|
||||
.max_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A min function for f64's without NaNs
|
||||
pub fn min(vals: &[f64]) -> f64 {
|
||||
*vals
|
||||
.iter()
|
||||
.min_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max() {
|
||||
let assert_float_eq = |a: f64, b: f64| {
|
||||
assert!((a - b).abs() < f64::EPSILON);
|
||||
};
|
||||
|
||||
assert_float_eq(1.0, max(&[1.0]));
|
||||
assert_float_eq(-1.0, max(&[-1.0]));
|
||||
assert_float_eq(-1.0, max(&[-2.0, -1.0]));
|
||||
assert_float_eq(1.0, max(&[-1.0, 1.0]));
|
||||
assert_float_eq(1.0, max(&[-1.0, 1.0, 0.0]));
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod min_max;
|
||||
pub mod number;
|
||||
pub mod units;
|
||||
@@ -0,0 +1,48 @@
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
use rust_decimal::Decimal;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]
|
||||
#[serde(untagged)]
|
||||
pub enum Number {
|
||||
Int(i32),
|
||||
Decimal(Decimal),
|
||||
}
|
||||
|
||||
impl fmt::Display for Number {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Number::Int(i) => fmt::Display::fmt(&i, f),
|
||||
Number::Decimal(i) => fmt::Display::fmt(&i, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for Number {
|
||||
fn from(x: i32) -> Number {
|
||||
Number::Int(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Decimal> for Number {
|
||||
fn from(x: Decimal) -> Number {
|
||||
Number::Decimal(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Number> for usize {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(numeric: Number) -> Result<Self, Self::Error> {
|
||||
match numeric {
|
||||
Number::Int(i) => usize::try_from(i).map_err(|_| ()),
|
||||
Number::Decimal(d) => match d.to_u64() {
|
||||
Some(u) => usize::try_from(u).map_err(|_| ()),
|
||||
None => Err(()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//! This module contains common units.
|
||||
|
||||
pub type Scalar = f64;
|
||||
|
||||
/// Type alias for unit of time
|
||||
pub type Second = Scalar;
|
||||
|
||||
/// Supported time units
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Unit {
|
||||
Second,
|
||||
MilliSecond,
|
||||
}
|
||||
|
||||
impl Unit {
|
||||
/// The abbreviation of the Unit.
|
||||
pub fn short_name(self) -> String {
|
||||
match self {
|
||||
Unit::Second => String::from("s"),
|
||||
Unit::MilliSecond => String::from("ms"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the Second value formatted for the Unit.
|
||||
pub fn format(self, value: Second) -> String {
|
||||
match self {
|
||||
Unit::Second => format!("{:.3}", value),
|
||||
Unit::MilliSecond => format!("{:.1}", value * 1e3),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unit_short_name() {
|
||||
assert_eq!("s", Unit::Second.short_name());
|
||||
assert_eq!("ms", Unit::MilliSecond.short_name());
|
||||
}
|
||||
|
||||
// Note - the values are rounded when formatted.
|
||||
#[test]
|
||||
fn test_unit_format() {
|
||||
let value: Second = 123.456789;
|
||||
assert_eq!("123.457", Unit::Second.format(value));
|
||||
assert_eq!("123456.8", Unit::MilliSecond.format(value));
|
||||
}
|
||||
Reference in new issue
Block a user