pub struct Rate<T, const NOM: u64, const DENOM: u64> { /* private fields */ }Expand description
Reexport fugit
Represents a frequency.
The generic T can either be u32 or u64, and the const generics represent the ratio of the
raw contained within the rate: rate in Hz = NOM / DENOM * raw
Implementations§
Source§impl<const NOM: u64, const DENOM: u64> Rate<u32, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> Rate<u32, NOM, DENOM>
Sourcepub const fn from_raw(raw: u32) -> Rate<u32, NOM, DENOM>
pub const fn from_raw(raw: u32) -> Rate<u32, NOM, DENOM>
Create a Rate from a raw value.
let _d = Rate::<u32, 1, 1_000>::from_raw(1);Sourcepub const fn to_raw(&self) -> u32
pub const fn to_raw(&self) -> u32
Extract the raw value from a Rate.
let d = Rate::<u32, 1, 1_000>::from_raw(234);
assert_eq!(d.to_raw(), 234);Sourcepub const fn checked_add<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u32, O_NOM, O_DENOM>,
) -> Option<Rate<u32, NOM, DENOM>>
pub const fn checked_add<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u32, O_NOM, O_DENOM>, ) -> Option<Rate<u32, NOM, DENOM>>
Add two rates.
Returns None on raw overflow or cross-base conversion overflow.
let r1 = Rate::<u32, 1, 1_000>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(2);
let r3 = Rate::<u32, 1, 1_000>::from_raw(u32::MAX);
assert_eq!(r1.checked_add(r2).unwrap().to_raw(), 3);
assert_eq!(r1.checked_add(r3), None);Sourcepub const fn checked_sub<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u32, O_NOM, O_DENOM>,
) -> Option<Rate<u32, NOM, DENOM>>
pub const fn checked_sub<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u32, O_NOM, O_DENOM>, ) -> Option<Rate<u32, NOM, DENOM>>
Subtract two rates.
Returns None on raw underflow or cross-base conversion overflow.
let r1 = Rate::<u32, 1, 1_000>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(2);
let r3 = Rate::<u32, 1, 1_000>::from_raw(u32::MAX);
assert_eq!(r2.checked_sub(r1).unwrap().to_raw(), 1);
assert_eq!(r1.checked_sub(r3), None);Sourcepub const fn checked_rem<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u32, O_NOM, O_DENOM>,
) -> Option<Rate<u32, NOM, DENOM>>
pub const fn checked_rem<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u32, O_NOM, O_DENOM>, ) -> Option<Rate<u32, NOM, DENOM>>
Remainder of dividing two rates.
Returns None if other is zero or the cross-base conversion overflows.
let r1 = Rate::<u32, 1, 1_000>::from_raw(10);
let r2 = Rate::<u32, 1, 1_000>::from_raw(3);
assert_eq!(r1.checked_rem(r2).unwrap().to_raw(), 1);Sourcepub const fn const_partial_cmp<const R_NOM: u64, const R_DENOM: u64>(
self,
other: Rate<u32, R_NOM, R_DENOM>,
) -> Option<Ordering>
pub const fn const_partial_cmp<const R_NOM: u64, const R_DENOM: u64>( self, other: Rate<u32, R_NOM, R_DENOM>, ) -> Option<Ordering>
Const partial comparison.
Returns None if either side’s raw value cannot be expressed in
the common base without overflowing the storage type.
let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(1);
assert_eq!(r1.const_partial_cmp(r2), Some(core::cmp::Ordering::Greater));Sourcepub const fn const_eq<const R_NOM: u64, const R_DENOM: u64>(
self,
other: Rate<u32, R_NOM, R_DENOM>,
) -> bool
pub const fn const_eq<const R_NOM: u64, const R_DENOM: u64>( self, other: Rate<u32, R_NOM, R_DENOM>, ) -> bool
Const equality check.
Returns false (rather than panicking) if the cross-base
conversion overflows the storage type.
let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::from_raw(10);
assert!(r1.const_eq(r2));Sourcepub const fn const_try_from<const I_NOM: u64, const I_DENOM: u64>(
rate: Rate<u32, I_NOM, I_DENOM>,
) -> Option<Rate<u32, NOM, DENOM>>
pub const fn const_try_from<const I_NOM: u64, const I_DENOM: u64>( rate: Rate<u32, I_NOM, I_DENOM>, ) -> Option<Rate<u32, NOM, DENOM>>
Const try from, checking for overflow.
let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2 = Rate::<u32, 1, 1_000>::const_try_from(r1);
assert_eq!(r2.unwrap().to_raw(), 10);Sourcepub const fn const_try_into<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Option<Rate<u32, O_NOM, O_DENOM>>
pub const fn const_try_into<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Option<Rate<u32, O_NOM, O_DENOM>>
Const try into, checking for overflow.
let r1 = Rate::<u32, 1, 1_00>::from_raw(1);
let r2: Option<Rate::<u32, 1, 1_000>> = r1.const_try_into();
assert_eq!(r2.unwrap().to_raw(), 10);Sourcepub const fn try_to_duration<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Option<Duration<u32, O_NOM, O_DENOM>>
pub const fn try_to_duration<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Option<Duration<u32, O_NOM, O_DENOM>>
Convert to a duration. Returns None if this rate is zero.
let r1 = Rate::<u32, 1, 1>::from_raw(1);
let d1: Option<Duration::<u32, 1, 1_000>> = r1.try_to_duration();
assert_eq!(d1.unwrap().as_ticks(), 1_000);Sourcepub const fn to_duration<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Duration<u32, O_NOM, O_DENOM>
pub const fn to_duration<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Duration<u32, O_NOM, O_DENOM>
Convert to a duration. Panics if this rate is zero.
Sourcepub const fn try_from_duration<const I_NOM: u64, const I_DENOM: u64>(
duration: Duration<u32, I_NOM, I_DENOM>,
) -> Option<Rate<u32, NOM, DENOM>>
pub const fn try_from_duration<const I_NOM: u64, const I_DENOM: u64>( duration: Duration<u32, I_NOM, I_DENOM>, ) -> Option<Rate<u32, NOM, DENOM>>
Convert from a duration. Returns None if the duration is zero.
let d1 = Duration::<u32, 1, 1_000>::from_ticks(2);
let r1 = Rate::<u32, 1, 1>::try_from_duration(d1);
assert_eq!(r1.unwrap().to_raw(), 500);Sourcepub const fn from_duration<const I_NOM: u64, const I_DENOM: u64>(
duration: Duration<u32, I_NOM, I_DENOM>,
) -> Rate<u32, NOM, DENOM>
pub const fn from_duration<const I_NOM: u64, const I_DENOM: u64>( duration: Duration<u32, I_NOM, I_DENOM>, ) -> Rate<u32, NOM, DENOM>
Convert from a duration. Panics if the duration is zero.
Sourcepub const fn convert<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Rate<u32, O_NOM, O_DENOM>
pub const fn convert<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Rate<u32, O_NOM, O_DENOM>
Convert between bases for a rate.
Unfortunately not a From impl due to collision with the std lib.
let r1 = Rate::<u32, 1, 100>::from_raw(1);
let r2: Rate::<u32, 1, 1_000> = r1.convert();
assert_eq!(r2.to_raw(), 10);Can be used in const contexts. Compilation will fail if the conversion causes overflow
const RAW: u32= u32::MAX - 10;
const R1: Rate::<u32, 1, 100> = Rate::<u32, 1, 100>::from_raw(RAW);
// Fails conversion due to overflow
const R2: Rate::<u32, 1, 200> = R1.convert();Sourcepub const fn to_Hz(&self) -> u32
pub const fn to_Hz(&self) -> u32
Convert the Rate to an interger number of Hz.
Panics if the result overflows the storage type.
Sourcepub const fn to_kHz(&self) -> u32
pub const fn to_kHz(&self) -> u32
Convert the Rate to an interger number of kHz.
Panics if the result overflows the storage type.
Sourcepub const fn to_MHz(&self) -> u32
pub const fn to_MHz(&self) -> u32
Convert the Rate to an interger number of MHz.
Panics if the result overflows the storage type.
Sourcepub const fn Hz(val: u32) -> Rate<u32, NOM, DENOM>
pub const fn Hz(val: u32) -> Rate<u32, NOM, DENOM>
Shorthand for creating a rate which represents hertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn kHz(val: u32) -> Rate<u32, NOM, DENOM>
pub const fn kHz(val: u32) -> Rate<u32, NOM, DENOM>
Shorthand for creating a rate which represents kilohertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn MHz(val: u32) -> Rate<u32, NOM, DENOM>
pub const fn MHz(val: u32) -> Rate<u32, NOM, DENOM>
Shorthand for creating a rate which represents megahertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn nanos(val: u32) -> Rate<u32, NOM, DENOM>
pub const fn nanos(val: u32) -> Rate<u32, NOM, DENOM>
Rate from a nanosecond period. Panics if val is zero.
Source§impl<const NOM: u64, const DENOM: u64> Rate<u64, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> Rate<u64, NOM, DENOM>
Sourcepub const fn from_raw(raw: u64) -> Rate<u64, NOM, DENOM>
pub const fn from_raw(raw: u64) -> Rate<u64, NOM, DENOM>
Create a Rate from a raw value.
let _d = Rate::<u64, 1, 1_000>::from_raw(1);Sourcepub const fn to_raw(&self) -> u64
pub const fn to_raw(&self) -> u64
Extract the raw value from a Rate.
let d = Rate::<u64, 1, 1_000>::from_raw(234);
assert_eq!(d.to_raw(), 234);Sourcepub const fn checked_add<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u64, O_NOM, O_DENOM>,
) -> Option<Rate<u64, NOM, DENOM>>
pub const fn checked_add<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u64, O_NOM, O_DENOM>, ) -> Option<Rate<u64, NOM, DENOM>>
Add two rates.
Returns None on raw overflow or cross-base conversion overflow.
let r1 = Rate::<u64, 1, 1_000>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(2);
let r3 = Rate::<u64, 1, 1_000>::from_raw(u64::MAX);
assert_eq!(r1.checked_add(r2).unwrap().to_raw(), 3);
assert_eq!(r1.checked_add(r3), None);Sourcepub const fn checked_sub<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u64, O_NOM, O_DENOM>,
) -> Option<Rate<u64, NOM, DENOM>>
pub const fn checked_sub<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u64, O_NOM, O_DENOM>, ) -> Option<Rate<u64, NOM, DENOM>>
Subtract two rates.
Returns None on raw underflow or cross-base conversion overflow.
let r1 = Rate::<u64, 1, 1_000>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(2);
let r3 = Rate::<u64, 1, 1_000>::from_raw(u64::MAX);
assert_eq!(r2.checked_sub(r1).unwrap().to_raw(), 1);
assert_eq!(r1.checked_sub(r3), None);Sourcepub const fn checked_rem<const O_NOM: u64, const O_DENOM: u64>(
self,
other: Rate<u64, O_NOM, O_DENOM>,
) -> Option<Rate<u64, NOM, DENOM>>
pub const fn checked_rem<const O_NOM: u64, const O_DENOM: u64>( self, other: Rate<u64, O_NOM, O_DENOM>, ) -> Option<Rate<u64, NOM, DENOM>>
Remainder of dividing two rates.
Returns None if other is zero or the cross-base conversion overflows.
let r1 = Rate::<u64, 1, 1_000>::from_raw(10);
let r2 = Rate::<u64, 1, 1_000>::from_raw(3);
assert_eq!(r1.checked_rem(r2).unwrap().to_raw(), 1);Sourcepub const fn const_partial_cmp<const R_NOM: u64, const R_DENOM: u64>(
self,
other: Rate<u64, R_NOM, R_DENOM>,
) -> Option<Ordering>
pub const fn const_partial_cmp<const R_NOM: u64, const R_DENOM: u64>( self, other: Rate<u64, R_NOM, R_DENOM>, ) -> Option<Ordering>
Const partial comparison.
Returns None if either side’s raw value cannot be expressed in
the common base without overflowing the storage type.
let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(1);
assert_eq!(r1.const_partial_cmp(r2), Some(core::cmp::Ordering::Greater));Sourcepub const fn const_eq<const R_NOM: u64, const R_DENOM: u64>(
self,
other: Rate<u64, R_NOM, R_DENOM>,
) -> bool
pub const fn const_eq<const R_NOM: u64, const R_DENOM: u64>( self, other: Rate<u64, R_NOM, R_DENOM>, ) -> bool
Const equality check.
Returns false (rather than panicking) if the cross-base
conversion overflows the storage type.
let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::from_raw(10);
assert!(r1.const_eq(r2));Sourcepub const fn const_try_from<const I_NOM: u64, const I_DENOM: u64>(
rate: Rate<u64, I_NOM, I_DENOM>,
) -> Option<Rate<u64, NOM, DENOM>>
pub const fn const_try_from<const I_NOM: u64, const I_DENOM: u64>( rate: Rate<u64, I_NOM, I_DENOM>, ) -> Option<Rate<u64, NOM, DENOM>>
Const try from, checking for overflow.
let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2 = Rate::<u64, 1, 1_000>::const_try_from(r1);
assert_eq!(r2.unwrap().to_raw(), 10);Sourcepub const fn const_try_into<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Option<Rate<u64, O_NOM, O_DENOM>>
pub const fn const_try_into<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Option<Rate<u64, O_NOM, O_DENOM>>
Const try into, checking for overflow.
let r1 = Rate::<u64, 1, 1_00>::from_raw(1);
let r2: Option<Rate::<u64, 1, 1_000>> = r1.const_try_into();
assert_eq!(r2.unwrap().to_raw(), 10);Sourcepub const fn try_to_duration<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Option<Duration<u64, O_NOM, O_DENOM>>
pub const fn try_to_duration<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Option<Duration<u64, O_NOM, O_DENOM>>
Convert to a duration. Returns None if this rate is zero.
let r1 = Rate::<u64, 1, 1>::from_raw(1);
let d1: Option<Duration::<u64, 1, 1_000>> = r1.try_to_duration();
assert_eq!(d1.unwrap().as_ticks(), 1_000);Sourcepub const fn to_duration<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Duration<u64, O_NOM, O_DENOM>
pub const fn to_duration<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Duration<u64, O_NOM, O_DENOM>
Convert to a duration. Panics if this rate is zero.
Sourcepub const fn try_from_duration<const I_NOM: u64, const I_DENOM: u64>(
duration: Duration<u64, I_NOM, I_DENOM>,
) -> Option<Rate<u64, NOM, DENOM>>
pub const fn try_from_duration<const I_NOM: u64, const I_DENOM: u64>( duration: Duration<u64, I_NOM, I_DENOM>, ) -> Option<Rate<u64, NOM, DENOM>>
Convert from a duration. Returns None if the duration is zero.
let d1 = Duration::<u64, 1, 1_000>::from_ticks(2);
let r1 = Rate::<u64, 1, 1>::try_from_duration(d1);
assert_eq!(r1.unwrap().to_raw(), 500);Sourcepub const fn from_duration<const I_NOM: u64, const I_DENOM: u64>(
duration: Duration<u64, I_NOM, I_DENOM>,
) -> Rate<u64, NOM, DENOM>
pub const fn from_duration<const I_NOM: u64, const I_DENOM: u64>( duration: Duration<u64, I_NOM, I_DENOM>, ) -> Rate<u64, NOM, DENOM>
Convert from a duration. Panics if the duration is zero.
Sourcepub const fn convert<const O_NOM: u64, const O_DENOM: u64>(
self,
) -> Rate<u64, O_NOM, O_DENOM>
pub const fn convert<const O_NOM: u64, const O_DENOM: u64>( self, ) -> Rate<u64, O_NOM, O_DENOM>
Convert between bases for a rate.
Unfortunately not a From impl due to collision with the std lib.
let r1 = Rate::<u64, 1, 100>::from_raw(1);
let r2: Rate::<u64, 1, 1_000> = r1.convert();
assert_eq!(r2.to_raw(), 10);Can be used in const contexts. Compilation will fail if the conversion causes overflow
const RAW: u64= u64::MAX - 10;
const R1: Rate::<u64, 1, 100> = Rate::<u64, 1, 100>::from_raw(RAW);
// Fails conversion due to overflow
const R2: Rate::<u64, 1, 200> = R1.convert();Sourcepub const fn to_Hz(&self) -> u64
pub const fn to_Hz(&self) -> u64
Convert the Rate to an interger number of Hz.
Panics if the result overflows the storage type.
Sourcepub const fn to_kHz(&self) -> u64
pub const fn to_kHz(&self) -> u64
Convert the Rate to an interger number of kHz.
Panics if the result overflows the storage type.
Sourcepub const fn to_MHz(&self) -> u64
pub const fn to_MHz(&self) -> u64
Convert the Rate to an interger number of MHz.
Panics if the result overflows the storage type.
Sourcepub const fn Hz(val: u64) -> Rate<u64, NOM, DENOM>
pub const fn Hz(val: u64) -> Rate<u64, NOM, DENOM>
Shorthand for creating a rate which represents hertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn kHz(val: u64) -> Rate<u64, NOM, DENOM>
pub const fn kHz(val: u64) -> Rate<u64, NOM, DENOM>
Shorthand for creating a rate which represents kilohertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn MHz(val: u64) -> Rate<u64, NOM, DENOM>
pub const fn MHz(val: u64) -> Rate<u64, NOM, DENOM>
Shorthand for creating a rate which represents megahertz.
Panics if the resulting raw value overflows the storage type.
Sourcepub const fn nanos(val: u64) -> Rate<u64, NOM, DENOM>
pub const fn nanos(val: u64) -> Rate<u64, NOM, DENOM>
Rate from a nanosecond period. Panics if val is zero.
Trait Implementations§
Source§impl<const NOM: u64, const DENOM: u64> AddAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> AddAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
Source§impl<const L_NOM: u64, const L_DENOM: u64, const R_NOM: u64, const R_DENOM: u64> Div<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u64, const L_DENOM: u64, const R_NOM: u64, const R_DENOM: u64> Div<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
Source§impl<const L_NOM: u64, const L_DENOM: u64, const R_NOM: u64, const R_DENOM: u64> Div<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u64, const L_DENOM: u64, const R_NOM: u64, const R_DENOM: u64> Div<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
Source§impl<const NOM: u64, const DENOM: u64> DivAssign<u32> for Rate<u32, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> DivAssign<u32> for Rate<u32, NOM, DENOM>
Source§fn div_assign(&mut self, other: u32)
fn div_assign(&mut self, other: u32)
/= operation. Read moreSource§impl<const NOM: u64, const DENOM: u64> DivAssign<u32> for Rate<u64, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> DivAssign<u32> for Rate<u64, NOM, DENOM>
Source§fn div_assign(&mut self, other: u32)
fn div_assign(&mut self, other: u32)
/= operation. Read moreSource§impl<const NOM: u64, const DENOM: u64> MulAssign<u32> for Rate<u32, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> MulAssign<u32> for Rate<u32, NOM, DENOM>
Source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
*= operation. Read moreSource§impl<const NOM: u64, const DENOM: u64> MulAssign<u32> for Rate<u64, NOM, DENOM>
impl<const NOM: u64, const DENOM: u64> MulAssign<u32> for Rate<u64, NOM, DENOM>
Source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
*= operation. Read more