apsis-rs/crates/foundation/apsis-units/src/frequency.rs

66 lines
1.3 KiB
Rust

//! Frequency quantities.
//!
//! The base unit is the hertz, one cycle per second. [`Hertz`] measures
//! [`Frequency`](crate::dimension::Frequency).
use crate::dimension::Frequency;
use crate::macros::impl_quantity_ops;
use apsis_core::{Quantity, Unit};
use core::fmt;
/// A frequency in hertz.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Hertz(f64);
impl Hertz {
/// Creates a frequency from a value in hertz.
pub const fn new(value: f64) -> Self {
Self(value)
}
/// Returns this frequency in hertz.
pub const fn value(self) -> f64 {
self.0
}
/// Returns the period corresponding to this frequency, in seconds.
///
/// Returns `f64::INFINITY` when the frequency is zero, which is the
/// correct limit of an infinite period.
pub fn period(self) -> f64 {
1.0 / self.0
}
}
impl Unit for Hertz {
type Dimension = Frequency;
fn factor_to_base() -> f64 {
1.0
}
fn symbol() -> &'static str {
"Hz"
}
}
impl Quantity for Hertz {
type U = Hertz;
fn value(self) -> f64 {
self.0
}
fn from_value(value: f64) -> Self {
Self(value)
}
}
impl fmt::Display for Hertz {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} Hz", self.0)
}
}
impl_quantity_ops!(Hertz);