use crate::prelude::*;
use core::ops::Mul;
use float_eq::float_eq;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Copy, Clone)]
pub struct Frequency(pub f32);
impl Frequency {
pub fn from_hertz(value: f32) -> Self {
Self(value)
}
pub fn hertz(&self) -> f32 {
self.0
}
}
impl From<f32> for Frequency {
fn from(value: f32) -> Self {
Frequency(value)
}
}
impl From<Frequency> for f32 {
fn from(value: Frequency) -> Self {
value.0
}
}
impl PartialEq for Frequency {
fn eq(&self, other: &Self) -> bool {
float_eq!(self.0, other.0, abs <= 0.000_1)
}
}
impl Eq for Frequency {}
impl Mul for Frequency {
type Output = Frequency;
fn mul(self, rhs: Self) -> Self::Output {
Frequency(self.0 * rhs.0)
}
}
impl Mul<f32> for Frequency {
type Output = Frequency;
fn mul(self, rhs: f32) -> Self::Output {
Frequency(self.0 * rhs)
}
}
impl Hash for Frequency {
fn hash<H: Hasher>(&self, hasher: &mut H) {
let bits = if self.0.is_nan() {
0x7fc00000
} else {
(self.0 + 0.0).to_bits()
};
bits.hash(hasher);
}
}