[−][src]Trait embedded_time::duration::Duration
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 const PERIOD: Period = Period::new_raw(1, 1_000); // set LSbit period to 1 millisecond 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
Loading content...Associated Constants
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
assert_eq!(Seconds(123).count(), 123);
Provided methods
fn from_ticks<Rep>(ticks: Rep, period: Period) -> Option<Self> where
Self::Rep: TimeRep + TryFrom<Rep>,
Rep: TimeRep,
Self::Rep: TimeRep + TryFrom<Rep>,
Rep: TimeRep,
assert_eq!(Microseconds::<i32>::from_ticks(5_i64, Period::new_raw(1, 1_000)), Some(Microseconds(5_000_i32))); assert_eq!(Microseconds::<i64>::from_ticks(i32::MAX, Period::new_raw(1, 1_000)), Some(Microseconds((i32::MAX as i64) * 1_000))); 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)));
fn into_ticks<Rep>(self, period: Period) -> Option<Rep> where
Self::Rep: TimeRep,
Rep: TimeRep + TryFrom<Self::Rep>,
Self::Rep: TimeRep,
Rep: TimeRep + TryFrom<Self::Rep>,
Create an integer representation with LSbit period of that provided
Errors
- the conversion of periods causes an overflow
- the Self integer cast to that of the provided type fails
Examples
assert_eq!(Microseconds(5_000_i32).into_ticks::<i32>(Period::new_raw(1, 1_000)), Some(5_i32)); assert_eq!(Microseconds(5_000_i32).into_ticks::<i32>(Period::new_raw(1, 200)), Some(1_i32)); assert_eq!(Microseconds::<i32>(i32::MAX).into_ticks::<i64>(Period::new_raw(1, 2_000_000)), Some((i32::MAX as i64) * 2)); assert_eq!(Microseconds::<i64>((i32::MAX as i64) + 2).into_ticks::<i32>(Period::new_raw(1, 500_000)), Some(i32::MAX / 2 + 1)); assert_eq!(Microseconds::<i64>(i32::MAX as i64).into_ticks::<i32>(Period::new_raw(1, 500_000)), Some(i32::MAX / 2));
#[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);
fn wrapping_sub<Rhs>(self, rhs: Rhs) -> Option<Self> where
Self: TryConvertFrom<Rhs>,
Self::Rep: TryFrom<Rhs::Rep, Error: Debug>,
Rhs::Rep: TimeRep,
Rhs: Duration,
Self: TryConvertFrom<Rhs>,
Self::Rep: TryFrom<Rhs::Rep, Error: Debug>,
Rhs::Rep: TimeRep,
Rhs: Duration,
Apply wrapping subtraction
Example
assert_eq!(Seconds(1).wrapping_sub(Seconds(u32::MAX as i32)), Some(Seconds(2)));