#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Bps(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Hertz(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct KiloHertz(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct MegaHertz(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Seconds(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Miliseconds(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Microseconds(pub u32);
pub trait U32Ext {
fn bps(self) -> Bps;
fn hz(self) -> Hertz;
fn khz(self) -> KiloHertz;
fn mhz(self) -> MegaHertz;
fn s(self) -> Seconds;
fn ms(self) -> Miliseconds;
fn us(self) -> Microseconds;
}
impl U32Ext for u32 {
fn bps(self) -> Bps {
Bps(self)
}
fn hz(self) -> Hertz {
Hertz(self)
}
fn khz(self) -> KiloHertz {
KiloHertz(self)
}
fn mhz(self) -> MegaHertz {
MegaHertz(self)
}
fn s(self) -> Seconds {
Seconds(self)
}
fn ms(self) -> Miliseconds {
Miliseconds(self)
}
fn us(self) -> Microseconds {
Microseconds(self)
}
}
impl Into<Hertz> for KiloHertz {
fn into(self) -> Hertz {
Hertz(self.0 * 1_000)
}
}
impl Into<Hertz> for MegaHertz {
fn into(self) -> Hertz {
Hertz(self.0 * 1_000_000)
}
}
impl Into<KiloHertz> for MegaHertz {
fn into(self) -> KiloHertz {
KiloHertz(self.0 * 1_000)
}
}
impl Into<KiloHertz> for Hertz {
fn into(self) -> KiloHertz {
KiloHertz(self.0 / 1_000)
}
}
impl Into<MegaHertz> for Hertz {
fn into(self) -> MegaHertz {
MegaHertz(self.0 / 1_000_000)
}
}
impl Into<MegaHertz> for KiloHertz {
fn into(self) -> MegaHertz {
MegaHertz(self.0 / 1_000)
}
}
impl Into<Miliseconds> for Seconds {
fn into(self) -> Miliseconds {
Miliseconds(self.0 * 1_000)
}
}
impl Into<Microseconds> for Seconds {
fn into(self) -> Microseconds {
Microseconds(self.0 * 1_000_000)
}
}
impl Into<Microseconds> for Miliseconds {
fn into(self) -> Microseconds {
Microseconds(self.0 * 1_000)
}
}
impl Into<Seconds> for Miliseconds {
fn into(self) -> Seconds {
Seconds(self.0 / 1_000)
}
}
impl Into<Seconds> for Microseconds {
fn into(self) -> Seconds {
Seconds(self.0 / 1_000_000)
}
}
impl Into<Miliseconds> for Microseconds {
fn into(self) -> Miliseconds {
Miliseconds(self.0 / 1_000)
}
}
impl Into<Hertz> for Bps {
fn into(self) -> Hertz {
Hertz(self.0)
}
}
impl Into<Hertz> for Microseconds {
fn into(self) -> Hertz {
Hertz(1_000_000_u32 / self.0)
}
}
impl Into<Microseconds> for Hertz {
fn into(self) -> Microseconds {
Microseconds(1_000_000_u32 / self.0)
}
}
#[cfg(test)]
mod tests {
use crate::time::*;
#[test]
fn convert_us_to_hz() {
let as_us: Microseconds = 3.hz().into();
assert_eq!(as_us.0, 333_333_u32);
}
#[test]
fn convert_ms_to_us() {
let as_us: Microseconds = 3.ms().into();
assert_eq!(as_us.0, 3_000_u32);
}
#[test]
fn convert_mhz_to_hz() {
let as_hz: Hertz = 48.mhz().into();
assert_eq!(as_hz.0, 48_000_000_u32);
}
}