[][src]Trait embedded_time::duration::Duration

pub trait Duration: Sized + Copy + Display {
    type Rep: TimeRep;

    const PERIOD: Period;

    fn new(value: Self::Rep) -> Self;
fn count(self) -> Self::Rep; fn from_ticks<Rep>(ticks: Rep, period: Period) -> Option<Self>
    where
        Self::Rep: TimeRep + TryFrom<Rep>,
        Rep: TimeRep
, { ... }
fn into_ticks<Rep>(self, period: Period) -> Option<Rep>
    where
        Self::Rep: TimeRep,
        Rep: TimeRep + TryFrom<Self::Rep>
, { ... }
#[must_use] fn min_value() -> Self::Rep { ... }
#[must_use] fn max_value() -> Self::Rep { ... } }

A duration of time with generic storage

Each implementation defines a constant fraction/ratio representing the period of the LSbit

Implementation Example

#[derive(Copy, Clone)]
struct Milliseconds<T: TimeRep>(pub T);

impl<T: TimeRep> Duration for Milliseconds<T> {
    type Rep = T;   // set the storage type

    // set LSbit period to 1 millisecond
    const PERIOD: Period = Period::new_raw(1, 1_000);

    fn new(value: Self::Rep) -> Self {
        Self(value)
    }

    fn count(self) -> Self::Rep {
        self.0
    }
}

impl<T: TimeRep> fmt::Display for Milliseconds<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        unimplemented!()
    }
     
}

Associated Types

type Rep: TimeRep

Loading content...

Associated Constants

const PERIOD: Period

Loading content...

Required methods

fn new(value: Self::Rep) -> Self

Not generally useful or needed as the duration can be instantiated like this:

Seconds(123);
123.seconds();

It only exists to allow Duration methods with default definitions to create a new duration

fn count(self) -> Self::Rep

Returns the integer value of the Duration

Examples

assert_eq!(Seconds(123).count(), 123);
Loading content...

Provided methods

fn from_ticks<Rep>(ticks: Rep, period: Period) -> Option<Self> where
    Self::Rep: TimeRep + TryFrom<Rep>,
    Rep: TimeRep

Constructs a Duration from a value of ticks and a period

Examples

assert_eq!(Microseconds::<i32>::from_ticks(5_i64, Period::new_raw(1, 1_000)),
    Some(Microseconds(5_000_i32)));

// the conversion arithmetic will not cause overflow
assert_eq!(Milliseconds::<i32>::from_ticks((i32::MAX as i64) + 1, Period::new_raw(1, 1_000_000)),
    Some(Milliseconds((((i32::MAX as i64) + 1) / 1_000) as i32)));

Errors

the conversion of periods causes an overflow:

assert_eq!(Milliseconds::<i32>::from_ticks(i32::MAX, Period::new_raw(1, 1)),
    None);

the Self integer cast to that of the provided type fails

assert_eq!(Seconds::<i32>::from_ticks(i32::MAX as i64 + 1, Period::new_raw(1, 1)),
    None);

Returns

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

fn into_ticks<Rep>(self, period: Period) -> Option<Rep> where
    Self::Rep: TimeRep,
    Rep: TimeRep + TryFrom<Self::Rep>, 

Create an integer representation with LSbit period of that provided

Examples

assert_eq!(Microseconds(5_000_i32).into_ticks::<i32>(Period::new_raw(1, 1_000)), Some(5_i32));

// the _into_ period can be any value
assert_eq!(Microseconds(5_000_i32).into_ticks::<i32>(Period::new_raw(1, 200)), Some(1_i32));

// as long as the result fits in the provided integer, it will succeed
assert_eq!(Microseconds::<i32>(i32::MAX).into_ticks::<i64>(Period::new_raw(1, 2_000_000)), Some((i32::MAX as i64) * 2));

Errors

the conversion of periods causes an overflow:

assert_eq!(Seconds(i32::MAX).into_ticks::<i32>(Period::new_raw(1, 1_000)), None);

the Self integer cast to that of the provided type fails

assert_eq!(Seconds(i32::MAX as i64 + 1).into_ticks::<i32>(Period::new_raw(1, 1)), None);

Returns

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

#[must_use]fn min_value() -> Self::Rep

assert_eq!(Seconds::<i32>::min_value(), i32::MIN);

#[must_use]fn max_value() -> Self::Rep

assert_eq!(Seconds::<i32>::max_value(), i32::MAX);
Loading content...

Implementors

impl<Rep: TimeRep> Duration for Hours<Rep>[src]

type Rep = Rep

impl<Rep: TimeRep> Duration for Microseconds<Rep>[src]

type Rep = Rep

impl<Rep: TimeRep> Duration for Milliseconds<Rep>[src]

type Rep = Rep

impl<Rep: TimeRep> Duration for Minutes<Rep>[src]

type Rep = Rep

impl<Rep: TimeRep> Duration for Nanoseconds<Rep>[src]

type Rep = Rep

impl<Rep: TimeRep> Duration for Seconds<Rep>[src]

type Rep = Rep

Loading content...