Skip to content

Commit

Permalink
Add more implementations of IntoF64 for standard numerical types.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Mar 16, 2024
1 parent dc2620b commit 8f79f70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions metrics/CHANGELOG.md
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion metrics/src/common.rs
Expand Up @@ -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<V: IntoF64>(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() {
Expand Down Expand Up @@ -294,4 +312,21 @@ mod tests {
assert_eq!(Some(variant), parsed);
}
}

#[test]
fn into_f64() {
fn test<T: IntoF64>(val: T) {
assert!(!val.into_f64().is_nan());
}

test::<i8>(1);
test::<u8>(1);
test::<i16>(1);
test::<u16>(1);
test::<i32>(1);
test::<u32>(1);
test::<f32>(1.0);
test::<f64>(1.0);
test::<Duration>(Duration::from_secs(1));
}
}

0 comments on commit 8f79f70

Please sign in to comment.