[][src]Struct embedded_time::Period

pub struct Period<T = u32>(_);

A fractional time period

Used primarily to define the period of one count of a Duration, Instant and Clock impl types but also convertible to/from Hertz.

The default inner type is u32.

Implementations

impl<T> Period<T>[src]

pub const fn new(numerator: T, denominator: T) -> Self[src]

Construct a new fractional Period.

A reduction is not performed. If reduction is needed, use Period::new_reduce()

pub const fn numerator(&self) -> &T[src]

Return the numerator of the fraction

pub const fn denominator(&self) -> &T[src]

Return the denominator of the fraction

impl<T: TimeInt> Period<T>[src]

pub fn new_reduce(numerator: T, denominator: T) -> Result<Self, ConversionError>[src]

Construct a new fractional Period.

A reduction is performed.

Errors

ConversionError::DivByZero : A 0 denominator was detected

pub fn to_integer(&self) -> T[src]

Returns the value truncated to an integer

pub fn from_integer(value: T) -> Self[src]

Constructs a Period from an integer.

Equivalent to Period::new(value,1).

pub fn recip(self) -> Self[src]

Returns the reciprocal of the fraction

pub fn checked_mul(&self, v: &Self) -> Result<Self, ConversionError>[src]

Checked Period * Period = Period

Examples

assert_eq!(<Period>::new(1000, 1).checked_mul(&<Period>::new(5,5)),
    Ok(<Period>::new(5_000, 5)));

assert_eq!(<Period>::new(u32::MAX, 1).checked_mul(&<Period>::new(2,1)),
    Err(ConversionError::Overflow));

Errors

ConversionError::Overflow

pub fn checked_div(&self, v: &Self) -> Result<Self, ConversionError>[src]

Checked Period / Period = Period

Examples

assert_eq!(<Period>::new(1000, 1).checked_div(&<Period>::new(10, 1000)),
    Ok(<Period>::new(1_000_000, 10)));

assert_eq!(<Period>::new(1, u32::MAX).checked_div(&<Period>::new(2,1)),
    Err(ConversionError::Overflow));

Errors

ConversionError::Overflow

pub fn checked_mul_integer(
    &self,
    multiplier: T
) -> Result<Self, ConversionError>
[src]

Checked Period * integer = Period

Examples

assert_eq!(<Period>::new(1000, 1).checked_mul_integer(5_u32),
    Ok(<Period>::new(5_000, 1)));

assert_eq!(<Period>::new(u32::MAX, 1).checked_mul_integer(2_u32),
    Err(ConversionError::Overflow));

Errors

ConversionError::Overflow

pub fn checked_div_integer(&self, divisor: T) -> Result<Self, ConversionError>[src]

Checked Period / integer = Period

Examples

assert_eq!(<Period>::new(1000, 1).checked_div_integer(5_u32),
    Ok(<Period>::new(200, 1)));

assert_eq!(<Period>::new(1, 1000).checked_div_integer(5_u32),
    Ok(<Period>::new(1, 5000)));

assert_eq!(<Period>::new(1, u32::MAX).checked_div_integer(2_u32),
    Err(ConversionError::Overflow));

Errors

ConversionError::Overflow

Trait Implementations

impl<T: Debug> Debug for Period<T>[src]

impl<T> Div<Period<T>> for Period<T> where
    T: TimeInt, 
[src]

type Output = Self

The resulting type after applying the / operator.

fn div(self, rhs: Self) -> Self::Output[src]

assert_eq!(Period::<u32>::new(1000, 1) / <Period>::new(10, 1_000),
    <Period>::new(1_000_000, 10));

impl<T> Mul<Period<T>> for Period<T> where
    T: TimeInt, 
[src]

type Output = Self

The resulting type after applying the * operator.

fn mul(self, rhs: Self) -> Self::Output[src]

assert_eq!(Period::<u32>::new(1000, 1) * <Period>::new(5,5),
    <Period>::new(5_000, 5));

impl<T: TimeInt> PartialEq<Period<T>> for Period<T>[src]

impl<T: TimeInt> PartialOrd<Period<T>> for Period<T>[src]

impl<T: TimeInt> TryFrom<Hertz<T>> for Period<T>[src]

type Error = ConversionError

The type returned in the event of a conversion error.

fn try_from(freq: Hertz<T>) -> Result<Self, Self::Error>[src]

Constructs a Period in seconds from a frequency in Hertz

Examples

assert_eq!(Period::try_from(Hertz(1_000_u32)),
    Ok(<Period>::new(1, 1_000)));

assert_eq!(Period::try_from(Hertz(0_u32)),
    Err(ConversionError::DivByZero));

assert_eq!(Hertz(1_000_u32).try_into(),
    Ok(<Period>::new(1, 1_000)));

Errors

ConversionError::DivByZero

impl<T: TimeInt> TryFrom<Period<T>> for Hertz<T>[src]

type Error = ConversionError

The type returned in the event of a conversion error.

fn try_from(period: Period<T>) -> Result<Self, Self::Error>[src]

Create a new Frequency from a fractional Period in seconds

Examples

assert_eq!(Hertz::try_from(<Period>::new(1, 1_000)),
    Ok(Hertz(1_000_u32)));

assert_eq!(Hertz::try_from(<Period>::new(0, 1_000)),
    Err(ConversionError::DivByZero));

assert_eq!(<Period>::new(1, 1_000).try_into(),
    Ok(Hertz(1_000_u32)));

Errors

ConversionError::DivByZero

Auto Trait Implementations

impl<T> RefUnwindSafe for Period<T> where
    T: RefUnwindSafe

impl<T> Send for Period<T> where
    T: Send

impl<T> Sync for Period<T> where
    T: Sync

impl<T> Unpin for Period<T> where
    T: Unpin

impl<T> UnwindSafe for Period<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.