embassy/embassy-stm32/src/time.rs
Sebastian Goll 4863f88d02 Make Hertz constructors const
This allows them to be used in constant values.
2023-04-13 00:06:14 +02:00

80 lines
1.6 KiB
Rust

//! Time units
use core::ops::{Div, Mul};
/// Hertz
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Hertz(pub u32);
impl Hertz {
pub const fn hz(hertz: u32) -> Self {
Self(hertz)
}
pub const fn khz(kilohertz: u32) -> Self {
Self(kilohertz * 1_000)
}
pub const fn mhz(megahertz: u32) -> Self {
Self(megahertz * 1_000_000)
}
}
/// This is a convenience shortcut for [`Hertz::hz`]
pub const fn hz(hertz: u32) -> Hertz {
Hertz::hz(hertz)
}
/// This is a convenience shortcut for [`Hertz::khz`]
pub const fn khz(kilohertz: u32) -> Hertz {
Hertz::khz(kilohertz)
}
/// This is a convenience shortcut for [`Hertz::mhz`]
pub const fn mhz(megahertz: u32) -> Hertz {
Hertz::mhz(megahertz)
}
impl Mul<u32> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u32) -> Self::Output {
Hertz(self.0 * rhs)
}
}
impl Div<u32> for Hertz {
type Output = Hertz;
fn div(self, rhs: u32) -> Self::Output {
Hertz(self.0 / rhs)
}
}
impl Mul<u16> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u16) -> Self::Output {
self * (rhs as u32)
}
}
impl Div<u16> for Hertz {
type Output = Hertz;
fn div(self, rhs: u16) -> Self::Output {
self / (rhs as u32)
}
}
impl Mul<u8> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u8) -> Self::Output {
self * (rhs as u32)
}
}
impl Div<u8> for Hertz {
type Output = Hertz;
fn div(self, rhs: u8) -> Self::Output {
self / (rhs as u32)
}
}