[−][src]Trait embedded_time::duration::TryConvertFrom
Attempt to convert from one duration type to another
This is basically a specialization of the TryFrom
trait.
Required methods
fn try_convert_from(other: Source) -> Result<Self, ConversionError>
Perform the conversion
Implementors
impl<Source: Duration, Dest: Duration> TryConvertFrom<Source> for Dest where
Dest::Rep: TryFrom<Source::Rep>,
[src]
Dest::Rep: TryFrom<Source::Rep>,
fn try_convert_from(source: Source) -> Result<Self, ConversionError>
[src]
Attempt to convert from one duration type to another
Both the inner type and/or the LSbit period (units) can be converted
Examples
assert_eq!(Seconds::<u32>::try_convert_from(Milliseconds(23_000_u64)), Ok(Seconds(23_u32))); assert_eq!(Seconds::<u64>::try_convert_from(Milliseconds(23_000_u32)), Ok(Seconds(23_u64))); assert_eq!(Seconds::<u32>::try_convert_from(Milliseconds(230_u32)), Ok(Seconds(0)));
Errors
Failure will only occur if the value does not fit in the selected destination type.
ConversionError::Overflow
: The conversion of periods causes an overflow:
assert_eq!(Milliseconds::<u32>::try_convert_from(Seconds(u32::MAX)), Err(ConversionError::Overflow));
ConversionError::ConversionFailure
: The Self integer cast to that of the destination
type fails:
assert_eq!(Seconds::<u32>::try_convert_from(Seconds(u32::MAX as u64 + 1)), Err(ConversionError::ConversionFailure));
However, these work because the sequence of cast/conversion adapts:
// period conversion applied first assert_eq!(Hours::<u32>::try_convert_from(Microseconds(3_600_000_000_u64)), Ok(Hours(1_u32))); // cast applied first assert_eq!(Microseconds::<u64>::try_convert_from(Hours(1_u32)), Ok(Microseconds(3_600_000_000_u64)));