[−][src]Trait embedded_time::duration::TryConvertInto
Attempt to convert from one duration type to another
This is basically a specialization of the TryInto
trait.
Required methods
fn try_convert_into(self) -> Result<Dest, ConversionError>
Perform the conversion
Implementors
impl<Source, Dest> TryConvertInto<Dest> for Source where
Source: Duration,
Dest: Duration + TryConvertFrom<Source>,
[src]
Source: Duration,
Dest: Duration + TryConvertFrom<Source>,
fn try_convert_into(self) -> Result<Dest, ConversionError>
[src]
The reciprocal of TryConvertFrom
Examples
assert_eq!(Seconds(23_u64).try_convert_into(), Ok(Seconds(23_u32))); assert_eq!(Seconds(23_u32).try_convert_into(), Ok(Seconds(23_u64))); assert_eq!(Milliseconds(23_000_u64).try_convert_into(), Ok(Seconds(23_u32)));
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:
use embedded_time::duration::TryConvertInto; assert_eq!(<Seconds<u32> as TryConvertInto<Milliseconds<u32>>>::try_convert_into(Seconds(u32::MAX)), Err(ConversionError::Overflow));
ConversionError::ConversionFailure
- The Self integer cast to that of the destination
type fails:
use embedded_time::duration::TryConvertInto; assert_eq!(<Seconds<u64> as TryConvertInto<Seconds<u32>>>::try_convert_into(Seconds(u32::MAX as u64 + 1)), Err(ConversionError::ConversionFailure));
However, the following work because the sequence of cast/conversion adapts:
// period conversion applied first assert_eq!(Microseconds(3_600_000_000_u64).try_convert_into(), Ok(Hours(1_u32))); // cast applied first assert_eq!(Hours(1_u32).try_convert_into(), Ok(Microseconds(3_600_000_000_u64)));