[−][src]Trait embedded_time::duration::Duration
An unsigned, fixed-point duration type
Each implementation defines an integer type and a Fraction
scaling factor.
Constructing a duration
assert_eq!(23_u32.milliseconds(), Milliseconds(23_u32));
Getting H:M:S.MS... Components
let duration = 38_238_479_u32.microseconds(); let hours = Hours::<u32>::try_convert_from(duration).unwrap(); let minutes = Minutes::<u32>::try_convert_from(duration).unwrap() % Hours(1_u32); let seconds = Seconds::<u32>::try_convert_from(duration).unwrap() % Minutes(1_u32); let milliseconds = Milliseconds::<u32>::try_convert_from(duration).unwrap() % Seconds(1_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
Duration
from an named Duration
(eg.
Milliseconds
)
Examples
assert_eq!(Seconds(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!(Seconds(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!(Seconds(u32::MAX as u64 + 1).try_into_generic::<u32>(Fraction::new(1, 1)), Err(ConversionError::ConversionFailure));
fn try_from_rate<Rate: Rate>(rate: Rate) -> Result<Self, ConversionError> where
Rate: FixedPoint,
u32: TryFrom<Rate::T>,
Self: FixedPoint,
Self::T: TryFrom<Rate::T>,
Rate: FixedPoint,
u32: TryFrom<Rate::T>,
Self: FixedPoint,
Self::T: TryFrom<Rate::T>,
Attempt to construct the given duration type from the given rate type
(the duration is equal to the reciprocal of the rate)
Examples
assert_eq!( Microseconds::<u32>::try_from_rate(Kilohertz(2_u32)), Ok(Microseconds(500_u32)) );
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!( Nanoseconds::<u32>::try_from_rate(u32::MAX.MHz()), Err(ConversionError::Overflow) );
ConversionError::ConversionFailure
: The integer conversion to that of the destination
type fails.
assert_eq!( Seconds::<u32>::try_from_rate((u32::MAX as u64 + 1).Hz()), Err(ConversionError::ConversionFailure) );
ConversionError::DivByZero
: The rate is 0
, therefore the reciprocal is undefined.
assert_eq!( Seconds::<u32>::try_from_rate(0_u32.Hz()), Err(ConversionError::DivByZero) );