pub struct Rate<T, const NOM: u32, const DENOM: u32> { /* private fields */ }
Expand description
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: u32, const DENOM: u32> Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Rate<u32, NOM, DENOM>
sourcepub const fn from_raw(raw: u32) -> Self
pub const fn from_raw(raw: u32) -> Self
Create a Rate
from a raw value.
let _d = Rate::<u32, 1, 1_000>::from_raw(1);
sourcepub const fn raw(&self) -> u32
pub const fn raw(&self) -> u32
Extract the raw value from a Rate
.
let d = Rate::<u32, 1, 1_000>::from_raw(234);
assert_eq!(d.raw(), 234);
sourcepub const fn checked_add<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Rate<u32, O_NOM, O_DENOM>,
) -> Option<Self>
pub const fn checked_add<const O_NOM: u32, const O_DENOM: u32>( self, other: Rate<u32, O_NOM, O_DENOM>, ) -> Option<Self>
Add two rates while checking for 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().raw(), 3);
assert_eq!(r1.checked_add(r3), None);
sourcepub const fn checked_sub<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Rate<u32, O_NOM, O_DENOM>,
) -> Option<Self>
pub const fn checked_sub<const O_NOM: u32, const O_DENOM: u32>( self, other: Rate<u32, O_NOM, O_DENOM>, ) -> Option<Self>
Subtract two rates while checking for 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().raw(), 1);
assert_eq!(r1.checked_sub(r3), None);
sourcepub const fn const_partial_cmp<const R_NOM: u32, const R_DENOM: u32>(
self,
other: Rate<u32, R_NOM, R_DENOM>,
) -> Option<Ordering>
pub const fn const_partial_cmp<const R_NOM: u32, const R_DENOM: u32>( self, other: Rate<u32, R_NOM, R_DENOM>, ) -> Option<Ordering>
Const partial comparison.
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: u32, const R_DENOM: u32>(
self,
other: Rate<u32, R_NOM, R_DENOM>,
) -> bool
pub const fn const_eq<const R_NOM: u32, const R_DENOM: u32>( self, other: Rate<u32, R_NOM, R_DENOM>, ) -> bool
Const equality check.
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: u32, const I_DENOM: u32>(
rate: Rate<u32, I_NOM, I_DENOM>,
) -> Option<Self>
pub const fn const_try_from<const I_NOM: u32, const I_DENOM: u32>( rate: Rate<u32, I_NOM, I_DENOM>, ) -> Option<Self>
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().raw(), 10);
sourcepub const fn const_try_into<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Option<Rate<u32, O_NOM, O_DENOM>>
pub const fn const_try_into<const O_NOM: u32, const O_DENOM: u32>( 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().raw(), 10);
sourcepub const fn try_into_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Option<Duration<u32, O_NOM, O_DENOM>>
pub const fn try_into_duration<const O_NOM: u32, const O_DENOM: u32>( self, ) -> Option<Duration<u32, O_NOM, O_DENOM>>
Const try into duration, checking for divide-by-zero.
let r1 = Rate::<u32, 1, 1>::from_raw(1);
let d1: Option<Duration::<u32, 1, 1_000>> = r1.try_into_duration();
assert_eq!(d1.unwrap().ticks(), 1_000);
sourcepub const fn into_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Duration<u32, O_NOM, O_DENOM>
pub const fn into_duration<const O_NOM: u32, const O_DENOM: u32>( self, ) -> Duration<u32, O_NOM, O_DENOM>
Convert from rate to duration.
sourcepub const fn try_from_duration<const I_NOM: u32, const I_DENOM: u32>(
duration: Duration<u32, I_NOM, I_DENOM>,
) -> Option<Self>
pub const fn try_from_duration<const I_NOM: u32, const I_DENOM: u32>( duration: Duration<u32, I_NOM, I_DENOM>, ) -> Option<Self>
Const try from duration, checking for divide-by-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().raw(), 500);
sourcepub const fn from_duration<const I_NOM: u32, const I_DENOM: u32>(
duration: Duration<u32, I_NOM, I_DENOM>,
) -> Self
pub const fn from_duration<const I_NOM: u32, const I_DENOM: u32>( duration: Duration<u32, I_NOM, I_DENOM>, ) -> Self
Convert from duration to rate.
sourcepub const fn convert<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Rate<u32, O_NOM, O_DENOM>
pub const fn convert<const O_NOM: u32, const O_DENOM: u32>( 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.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 nanos(val: u32) -> Self
pub const fn nanos(val: u32) -> Self
Shorthand for creating a rate which represents nanoseconds.
source§impl<const NOM: u32, const DENOM: u32> Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Rate<u64, NOM, DENOM>
sourcepub const fn from_raw(raw: u64) -> Self
pub const fn from_raw(raw: u64) -> Self
Create a Rate
from a raw value.
let _d = Rate::<u64, 1, 1_000>::from_raw(1);
sourcepub const fn raw(&self) -> u64
pub const fn raw(&self) -> u64
Extract the raw value from a Rate
.
let d = Rate::<u64, 1, 1_000>::from_raw(234);
assert_eq!(d.raw(), 234);
sourcepub const fn checked_add<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Rate<u64, O_NOM, O_DENOM>,
) -> Option<Self>
pub const fn checked_add<const O_NOM: u32, const O_DENOM: u32>( self, other: Rate<u64, O_NOM, O_DENOM>, ) -> Option<Self>
Add two rates while checking for 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().raw(), 3);
assert_eq!(r1.checked_add(r3), None);
sourcepub const fn checked_sub<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Rate<u64, O_NOM, O_DENOM>,
) -> Option<Self>
pub const fn checked_sub<const O_NOM: u32, const O_DENOM: u32>( self, other: Rate<u64, O_NOM, O_DENOM>, ) -> Option<Self>
Subtract two rates while checking for 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().raw(), 1);
assert_eq!(r1.checked_sub(r3), None);
sourcepub const fn const_partial_cmp<const R_NOM: u32, const R_DENOM: u32>(
self,
other: Rate<u64, R_NOM, R_DENOM>,
) -> Option<Ordering>
pub const fn const_partial_cmp<const R_NOM: u32, const R_DENOM: u32>( self, other: Rate<u64, R_NOM, R_DENOM>, ) -> Option<Ordering>
Const partial comparison.
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: u32, const R_DENOM: u32>(
self,
other: Rate<u64, R_NOM, R_DENOM>,
) -> bool
pub const fn const_eq<const R_NOM: u32, const R_DENOM: u32>( self, other: Rate<u64, R_NOM, R_DENOM>, ) -> bool
Const equality check.
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: u32, const I_DENOM: u32>(
rate: Rate<u64, I_NOM, I_DENOM>,
) -> Option<Self>
pub const fn const_try_from<const I_NOM: u32, const I_DENOM: u32>( rate: Rate<u64, I_NOM, I_DENOM>, ) -> Option<Self>
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().raw(), 10);
sourcepub const fn const_try_into<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Option<Rate<u64, O_NOM, O_DENOM>>
pub const fn const_try_into<const O_NOM: u32, const O_DENOM: u32>( 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().raw(), 10);
sourcepub const fn try_into_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Option<Duration<u64, O_NOM, O_DENOM>>
pub const fn try_into_duration<const O_NOM: u32, const O_DENOM: u32>( self, ) -> Option<Duration<u64, O_NOM, O_DENOM>>
Const try into duration, checking for divide-by-zero.
let r1 = Rate::<u64, 1, 1>::from_raw(1);
let d1: Option<Duration::<u64, 1, 1_000>> = r1.try_into_duration();
assert_eq!(d1.unwrap().ticks(), 1_000);
sourcepub const fn into_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Duration<u64, O_NOM, O_DENOM>
pub const fn into_duration<const O_NOM: u32, const O_DENOM: u32>( self, ) -> Duration<u64, O_NOM, O_DENOM>
Convert from rate to duration.
sourcepub const fn try_from_duration<const I_NOM: u32, const I_DENOM: u32>(
duration: Duration<u64, I_NOM, I_DENOM>,
) -> Option<Self>
pub const fn try_from_duration<const I_NOM: u32, const I_DENOM: u32>( duration: Duration<u64, I_NOM, I_DENOM>, ) -> Option<Self>
Const try from duration, checking for divide-by-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().raw(), 500);
sourcepub const fn from_duration<const I_NOM: u32, const I_DENOM: u32>(
duration: Duration<u64, I_NOM, I_DENOM>,
) -> Self
pub const fn from_duration<const I_NOM: u32, const I_DENOM: u32>( duration: Duration<u64, I_NOM, I_DENOM>, ) -> Self
Convert from duration to rate.
sourcepub const fn convert<const O_NOM: u32, const O_DENOM: u32>(
self,
) -> Rate<u64, O_NOM, O_DENOM>
pub const fn convert<const O_NOM: u32, const O_DENOM: u32>( 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.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 nanos(val: u64) -> Self
pub const fn nanos(val: u64) -> Self
Shorthand for creating a rate which represents nanoseconds.
Trait Implementations§
source§impl<const NOM: u32, const DENOM: u32> AddAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> AddAssign for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign for Rate<u32, NOM, DENOM>
source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moresource§impl<const NOM: u32, const DENOM: u32> AddAssign for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign for Rate<u64, NOM, DENOM>
source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moresource§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> Div<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> Div<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> Div<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> Div<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
source§impl<const NOM: u32, const DENOM: u32> DivAssign<u32> for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> 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: u32, const DENOM: u32> DivAssign<u32> for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> 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: u32, const DENOM: u32> MulAssign<u32> for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> 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: u32, const DENOM: u32> MulAssign<u32> for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> MulAssign<u32> for Rate<u64, NOM, DENOM>
source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
*=
operation. Read moresource§impl<const NOM: u32, const DENOM: u32> Ord for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Ord for Rate<u32, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Ord for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Ord for Rate<u64, NOM, DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u32, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u32, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u64, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u64, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialEq<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u32, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u32, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u32, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u64, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u64, R_NOM, R_DENOM>> for Rate<u32, L_NOM, L_DENOM>
source§impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32> PartialOrd<Rate<u64, R_NOM, R_DENOM>> for Rate<u64, L_NOM, L_DENOM>
source§impl<const NOM: u32, const DENOM: u32> SubAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> SubAssign<Rate<u32, NOM, DENOM>> for Rate<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> TryFrom<Rate<u64, NOM, DENOM>> for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> TryFrom<Rate<u64, NOM, DENOM>> for Rate<u32, NOM, DENOM>
impl<T: Copy, const NOM: u32, const DENOM: u32> Copy for Rate<T, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Eq for Rate<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Eq for Rate<u64, NOM, DENOM>
Auto Trait Implementations§
impl<T, const NOM: u32, const DENOM: u32> Freeze for Rate<T, NOM, DENOM>where
T: Freeze,
impl<T, const NOM: u32, const DENOM: u32> RefUnwindSafe for Rate<T, NOM, DENOM>where
T: RefUnwindSafe,
impl<T, const NOM: u32, const DENOM: u32> Send for Rate<T, NOM, DENOM>where
T: Send,
impl<T, const NOM: u32, const DENOM: u32> Sync for Rate<T, NOM, DENOM>where
T: Sync,
impl<T, const NOM: u32, const DENOM: u32> Unpin for Rate<T, NOM, DENOM>where
T: Unpin,
impl<T, const NOM: u32, const DENOM: u32> UnwindSafe for Rate<T, NOM, DENOM>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)