diff --git a/metrics/CHANGELOG.md b/metrics/CHANGELOG.md index 6636b233..e19942ab 100644 --- a/metrics/CHANGELOG.md +++ b/metrics/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - ReleaseDate +### Added + +- Additional implementations of `IntoF64` for standard numerical types (`i8`, `u8`, `i16`, `u16`, + `i32`, `u32`, and `f32`). + ## [0.22.2] - 2024-03-16 ### Fixed diff --git a/metrics/src/common.rs b/metrics/src/common.rs index 2cc1d7fd..bfd3856d 100644 --- a/metrics/src/common.rs +++ b/metrics/src/common.rs @@ -256,15 +256,33 @@ impl IntoF64 for core::time::Duration { } } +into_f64!(i8, u8, i16, u16, i32, u32, f32); + /// Helper method to allow monomorphization of values passed to the `histogram!` macro. #[doc(hidden)] pub fn __into_f64(value: V) -> f64 { value.into_f64() } +macro_rules! into_f64 { + ($($ty:ty),*) => { + $( + impl IntoF64 for $ty { + fn into_f64(self) -> f64 { + f64::from(self) + } + } + )* + }; +} + +pub(self) use into_f64; + #[cfg(test)] mod tests { - use super::Unit; + use std::time::Duration; + + use super::{IntoF64, Unit}; #[test] fn test_unit_conversions() { @@ -294,4 +312,21 @@ mod tests { assert_eq!(Some(variant), parsed); } } + + #[test] + fn into_f64() { + fn test(val: T) { + assert!(!val.into_f64().is_nan()); + } + + test::(1); + test::(1); + test::(1); + test::(1); + test::(1); + test::(1); + test::(1.0); + test::(1.0); + test::(Duration::from_secs(1)); + } }