[−][src]Trait embedded_time::rate::Rate
An unsigned, fixed-point rate type
Each implementation defines an integer type and a Fraction
scaling factor.
Constructing a rate
assert_eq!(45_u32.Hz(), Hertz(45_u32));
Provided methods
fn try_into_generic<DestInt: TimeInt>(
self,
scaling_factor: Fraction
) -> Result<Generic<DestInt>, ConversionError> where
Self: FixedPoint,
DestInt: TryFrom<Self::T>,
self,
scaling_factor: Fraction
) -> Result<Generic<DestInt>, ConversionError> where
Self: FixedPoint,
DestInt: TryFrom<Self::T>,
Construct a Generic
Rate
from an named Rate
Examples
assert_eq!(Hertz(2_u64).try_into_generic(Fraction::new(1,2_000)), Ok(Generic::new(4_000_u32, Fraction::new(1,2_000))));
Errors
Failure will only occur if the provided value does not fit in the selected destination type.
ConversionError::Overflow
: The conversion of the scaling factor causes an overflow.
assert_eq!(Hertz(u32::MAX).try_into_generic::<u32>(Fraction::new(1, 2)), Err(ConversionError::Overflow));
ConversionError::ConversionFailure
: The integer conversion to that of the destination
type fails.
assert_eq!(Hertz(u32::MAX as u64 + 1).try_into_generic::<u32>(Fraction::new(1, 1)), Err(ConversionError::ConversionFailure));
fn try_from_duration<Duration: Duration>(
duration: Duration
) -> Result<Self, ConversionError> where
Duration: FixedPoint,
u32: TryFrom<Duration::T>,
Self: FixedPoint,
Self::T: TryFrom<Duration::T>,
duration: Duration
) -> Result<Self, ConversionError> where
Duration: FixedPoint,
u32: TryFrom<Duration::T>,
Self: FixedPoint,
Self::T: TryFrom<Duration::T>,
Attempt to construct the given rate type from the given duration type
(the rate is equal to the reciprocal of the duration)
assert_eq!( Kilohertz::<u32>::try_from_duration(Microseconds(2_u32)), Ok(Kilohertz(500_u32)) );