Skip to content
Myr

Add unit information

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent d813652 · Browse files · 1 file+42 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+42 added−2 removed

  • other:+42 added−2 removed

1 file+42 added−2 removed

modified:src/benchmark/quantity.rs+42 added-2 removed
1 unmodified line
2 fmt::{self, Display, Formatter},3 ops::{Add, Div, Sub},4};56use serde::Serialize;6use serde::{ser::SerializeStruct, Serialize, Serializer};78pub trait IsQuantity {9 type Value;10
4 unmodified lines
1516 fn unsafe_from(value: Self::Value) -> Self;17}1819#[derive(Debug, Clone, Copy, Default, Serialize, PartialEq, Eq, PartialOrd)] // TODO: check partialord19#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd)] // TODO: check partialord20pub struct Quantity<21 Value,22 const IS_TIME: bool,23 const METRIC_PREFIX_TIME: i32,
59 unmodified lines
83 }8485 self.086 }8788 const fn unit_name_long(&self) -> &'static str {89 match (IS_TIME, IS_INFORMATION) {90 (true, false) => match METRIC_PREFIX_TIME {91 -6 => "microsecond",92 -3 => "millisecond",93 0 => "second",94 _ => unreachable!(),95 },96 (false, true) => match BINARY_PREFIX_INFORMATION {97 0 => "byte",98 10 => "kibibyte",99 20 => "mebibyte",100 30 => "gibibyte",101 40 => "tebibyte",102 _ => unreachable!(),103 },104 _ => unreachable!(),105 }106 }107}108109impl<const METRIC_PREFIX_TIME: i32> Time<METRIC_PREFIX_TIME> {110 pub fn convert_to<TargetUnit: IsQuantity<Value = f64>>(&self) -> TargetUnit {
52 unmodified lines
163 write!(f, "{} {}B", self.0, prefix)164 }165}166167impl<168 Value: Copy + Default + Serialize,169 const IS_TIME: bool,170 const METRIC_PREFIX_TIME: i32,171 const IS_INFORMATION: bool,172 const BINARY_PREFIX_INFORMATION: i32,173 > Serialize174 for Quantity<Value, IS_TIME, METRIC_PREFIX_TIME, IS_INFORMATION, BINARY_PREFIX_INFORMATION>175{176 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>177 where178 S: Serializer,179 {180 let mut state = serializer.serialize_struct("Quantity", 2)?;181 state.serialize_field("value", &self.0)?;182 state.serialize_field("unit", self.unit_name_long())?;183 state.end()184 }185}186187macro_rules! quantity_fn {188 ($name:ident, $unwrapped_values:ident, $body:expr) => {189 pub fn $name<190 const IS_TIME: bool,
55 unmodified lines