[][src]Trait embedded_time::duration::TryConvertInto

pub trait TryConvertInto<Dest> {
    fn try_convert_into(self) -> Option<Dest>;
}

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) -> Option<Dest>

Perform the conversion

Loading content...

Implementors

impl<Source, Dest> TryConvertInto<Dest> for Source where
    Source: Duration,
    Dest: Duration + TryConvertFrom<Source>, 
[src]

The reciprocal of TryConvertFrom

Examples

assert_eq!(Seconds(23_u64).try_convert_into(), Some(Seconds(23_u32)));
assert_eq!(Some(Seconds(23_u64)), (Seconds(23_u32).try_convert_into()));
assert_eq!(Milliseconds(23_000_u64).try_convert_into(), Some(Seconds(23_u32)));

Errors

the conversion of periods causes an overflow:

assert_eq!(Seconds(u32::MAX).try_convert_into(), None::<Milliseconds<u32>>);

the Self integer cast to that of the destination type fails

assert_eq!(Seconds(u32::MAX as u64 + 1).try_convert_into(), None::<Seconds<u32>>);

However, these work because the sequence of cast/conversion adapts

// period conversion applied first
assert_eq!(Microseconds(3_600_000_000_u64).try_convert_into(), Some(Hours(1_u32)));

// cast applied first
assert_eq!(Hours(1_u32).try_convert_into(), Some(Microseconds(3_600_000_000_u64)));

Returns

None if the result of the conversion does not fit in the requested Duration type

Loading content...