From 0bf7d50fdada955f9de0728beed2b168eda1735e Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 5 Oct 2026 14:22:17 +0200 Subject: [PATCH] Minor cleanup --- src/quantity/statistics.rs | 58 ++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/quantity/statistics.rs b/src/quantity/statistics.rs index ae21811..ffc9612 100644 --- a/src/quantity/statistics.rs +++ b/src/quantity/statistics.rs @@ -8,31 +8,6 @@ use uom::si::ratio::ratio; use super::{byte, second, Information, Time}; -pub trait UnsafeRawValue { - fn unsafe_raw_value(&self) -> f64; - fn unsafe_from_raw_value(value: f64) -> Self; -} - -impl UnsafeRawValue for Time { - fn unsafe_raw_value(&self) -> f64 { - self.get::() - } - - fn unsafe_from_raw_value(value: f64) -> Self { - Time::new::(value) - } -} - -impl UnsafeRawValue for Information { - fn unsafe_raw_value(&self) -> f64 { - self.get::() - } - - fn unsafe_from_raw_value(value: f64) -> Self { - Information::new::(value) - } -} - /// A min function that assumes no NaNs and at least one element pub fn min(values: impl IntoIterator) -> Q { values @@ -85,8 +60,35 @@ where } } -fn standard_deviation_f64(values: impl IntoIterator + Clone) -> f64 { - let mean_value = mean(values.clone()); +/// This is just sad, but after several hours trying to figure out how to write +/// a generic `standard_deviation` function using `uom` quantities, I gave up. +pub trait UnsafeRawValue { + fn unsafe_raw_value(&self) -> f64; + fn unsafe_from_raw_value(value: f64) -> Self; +} + +impl UnsafeRawValue for Time { + fn unsafe_raw_value(&self) -> f64 { + self.get::() + } + + fn unsafe_from_raw_value(value: f64) -> Self { + Time::new::(value) + } +} + +impl UnsafeRawValue for Information { + fn unsafe_raw_value(&self) -> f64 { + self.get::() + } + + fn unsafe_from_raw_value(value: f64) -> Self { + Information::new::(value) + } +} + +fn standard_deviation_f64(values: &[f64]) -> f64 { + let mean_value = mean(values.iter().copied()); let mut squared_deviations = 0.; let mut n = 0; @@ -101,7 +103,7 @@ fn standard_deviation_f64(values: impl IntoIterator + Clone) -> f64 pub fn standard_deviation(values: impl IntoIterator + Clone) -> Q { let values: Vec<_> = values.into_iter().map(|q| q.unsafe_raw_value()).collect(); - let result = standard_deviation_f64(values); + let result = standard_deviation_f64(&values); Q::unsafe_from_raw_value(result) }