From a119cbaf2d0b92993b7bc9ed9ab172e45b6a72a5 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 5 Oct 2026 14:16:44 +0200 Subject: [PATCH] Add unit information --- src/benchmark/quantity.rs | 44 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/benchmark/quantity.rs b/src/benchmark/quantity.rs index fc9780f..bc64c3f 100644 --- a/src/benchmark/quantity.rs +++ b/src/benchmark/quantity.rs @@ -3,7 +3,7 @@ use std::{ ops::{Add, Div, Sub}, }; -use serde::Serialize; +use serde::{ser::SerializeStruct, Serialize, Serializer}; pub trait IsQuantity { type Value; @@ -16,7 +16,7 @@ pub trait IsQuantity { fn unsafe_from(value: Self::Value) -> Self; } -#[derive(Debug, Clone, Copy, Default, Serialize, PartialEq, Eq, PartialOrd)] // TODO: check partialord +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd)] // TODO: check partialord pub struct Quantity< Value, const IS_TIME: bool, @@ -84,6 +84,26 @@ impl< self.0 } + + const fn unit_name_long(&self) -> &'static str { + match (IS_TIME, IS_INFORMATION) { + (true, false) => match METRIC_PREFIX_TIME { + -6 => "microsecond", + -3 => "millisecond", + 0 => "second", + _ => unreachable!(), + }, + (false, true) => match BINARY_PREFIX_INFORMATION { + 0 => "byte", + 10 => "kibibyte", + 20 => "mebibyte", + 30 => "gibibyte", + 40 => "tebibyte", + _ => unreachable!(), + }, + _ => unreachable!(), + } + } } impl Time { @@ -144,6 +164,26 @@ impl Display for Information Serialize + for Quantity +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut state = serializer.serialize_struct("Quantity", 2)?; + state.serialize_field("value", &self.0)?; + state.serialize_field("unit", self.unit_name_long())?; + state.end() + } +} + macro_rules! quantity_fn { ($name:ident, $unwrapped_values:ident, $body:expr) => { pub fn $name<