[][src]Trait embedded_time::duration::TryConvertFrom

pub trait TryConvertFrom<Source>: Sized {
    fn try_convert_from(other: Source) -> Option<Self>;
}

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

Perform the conversion

Loading content...

Implementors

impl<Source: Duration, Dest: Duration> TryConvertFrom<Source> for Dest where
    Dest::Rep: TryFrom<Source::Rep>, 
[src]

fn try_convert_from(source: Source) -> Option<Self>[src]

Attempt to convert from one duration type to another

Both the underlying storage type and/or the LSbit period can be converted

Examples

assert_eq!(Seconds::<u32>::try_convert_from(Milliseconds(23_000_u64)), Some(Seconds(23_u32)));
assert_eq!(Seconds::<u64>::try_convert_from(Milliseconds(23_000_u32)), Some(Seconds(23_u64)));
assert_eq!(Seconds::<u32>::try_convert_from(Milliseconds(230_u32)), Some(Seconds(0)));

Errors

the conversion of periods causes an overflow:

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

the Self integer cast to that of the provided type fails

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

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)), Some(Hours(1_u32)));

// cast applied first
assert_eq!(Microseconds::<u64>::try_convert_from(Hours(1_u32)), Some(Microseconds(3_600_000_000_u64)));

Returns

None if the result of the conversion does not fit in the requested integer size

Loading content...