[][src]Trait embedded_time::rate::Rate

pub trait Rate: Copy {
    fn try_into_generic<DestInt: TimeInt>(
        self,
        scaling_factor: Fraction
    ) -> Result<Generic<DestInt>, ConversionError>
    where
        Self: FixedPoint,
        DestInt: TryFrom<Self::T>
, { ... }
fn try_from_duration<Duration: Duration>(
        duration: Duration
    ) -> Result<Self, ConversionError>
    where
        Duration: FixedPoint,
        u32: TryFrom<Duration::T>,
        Self: FixedPoint,
        Self::T: TryFrom<Duration::T>
, { ... } }

An unsigned, fixed-point rate type

Each implementation defines an integer type and a Fraction scaling factor.

Constructing a rate

assert_eq!(45_u32.Hz(), Hertz(45_u32));

Provided methods

fn try_into_generic<DestInt: TimeInt>(
    self,
    scaling_factor: Fraction
) -> Result<Generic<DestInt>, ConversionError> where
    Self: FixedPoint,
    DestInt: TryFrom<Self::T>, 

Construct a Generic Rate from an named Rate

Examples

assert_eq!(Hertz(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!(Hertz(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!(Hertz(u32::MAX as u64 + 1).try_into_generic::<u32>(Fraction::new(1, 1)),
    Err(ConversionError::ConversionFailure));

fn try_from_duration<Duration: Duration>(
    duration: Duration
) -> Result<Self, ConversionError> where
    Duration: FixedPoint,
    u32: TryFrom<Duration::T>,
    Self: FixedPoint,
    Self::T: TryFrom<Duration::T>, 

Attempt to construct the given rate type from the given duration type

(the rate is equal to the reciprocal of the duration)

assert_eq!(
    Kilohertz::<u32>::try_from_duration(Microseconds(2_u32)),
    Ok(Kilohertz(500_u32))
);
Loading content...

Implementors

impl<T: TimeInt> Rate for Generic<T>[src]

impl<T: TimeInt> Rate for Baud<T>[src]

impl<T: TimeInt> Rate for BitsPerSecond<T>[src]

impl<T: TimeInt> Rate for BytesPerSecond<T>[src]

impl<T: TimeInt> Rate for Hertz<T>[src]

impl<T: TimeInt> Rate for Kibibaud<T>[src]

impl<T: TimeInt> Rate for KibibitsPerSecond<T>[src]

impl<T: TimeInt> Rate for KibibytesPerSecond<T>[src]

impl<T: TimeInt> Rate for KiloBytesPerSecond<T>[src]

impl<T: TimeInt> Rate for Kilobaud<T>[src]

impl<T: TimeInt> Rate for KilobitsPerSecond<T>[src]

impl<T: TimeInt> Rate for Kilohertz<T>[src]

impl<T: TimeInt> Rate for Mebibaud<T>[src]

impl<T: TimeInt> Rate for MebibitsPerSecond<T>[src]

impl<T: TimeInt> Rate for MebibytesPerSecond<T>[src]

impl<T: TimeInt> Rate for Megabaud<T>[src]

impl<T: TimeInt> Rate for MegabitsPerSecond<T>[src]

impl<T: TimeInt> Rate for MegabytesPerSecond<T>[src]

impl<T: TimeInt> Rate for Megahertz<T>[src]

Loading content...