[−][src]Trait embedded_time::duration::Duration
An unsigned duration of time
Each implementation defines a constant Period which is a fraction/ratio representing the
period of the count's LSbit
Constructing a duration
assert_eq!(Milliseconds::<u32>::new(23), Milliseconds(23_u32)); assert_eq!(23_u32.milliseconds(), Milliseconds(23_u32));
Get the integer count
assert_eq!(Milliseconds(23_u32).count(), 23_u32);
Formatting
Just forwards the underlying integer to core::fmt::Display::fmt()
assert_eq!(format!("{}", Seconds(123_u32)), "123");
Getting H:M:S.MS... Components
let duration = 38_238_479_u32.microseconds(); let hours = Hours::<u32>::try_convert_from(duration).unwrap(); let minutes = Minutes::<u32>::try_convert_from(duration).unwrap() % Hours(1_u32); let seconds = Seconds::<u32>::try_convert_from(duration).unwrap() % Minutes(1_u32); let milliseconds = Milliseconds::<u32>::try_convert_from(duration).unwrap() % Seconds(1_u32); // ...
Converting to core types
Examples
let core_duration = core::time::Duration::try_from(2_569_u32.milliseconds()).unwrap(); assert_eq!(core_duration.as_secs(), 2); assert_eq!(core_duration.subsec_nanos(), 569_000_000);
let core_duration: core::time::Duration = 2_569_u32.milliseconds().try_into().unwrap(); assert_eq!(core_duration.as_secs(), 2); assert_eq!(core_duration.subsec_nanos(), 569_000_000);
Converting from core types
Examples
let core_duration = core::time::Duration::new(5, 730023852); assert_eq!(Milliseconds::<u32>::try_from(core_duration), Ok(5_730.milliseconds()));
let duration: Result<Milliseconds<u32>, _> = core::time::Duration::new(5, 730023852).try_into(); assert_eq!(duration, Ok(5_730.milliseconds()));
Errors
ConversionError::ConversionFailure : The duration doesn't fit in the type specified
assert_eq!( Milliseconds::<u32>::try_from( core::time::Duration::from_millis((u32::MAX as u64) + 1)), Err(ConversionError::ConversionFailure)); let duration: Result<Milliseconds<u32>, _> = core::time::Duration::from_millis((u32::MAX as u64) + 1).try_into(); assert_eq!(duration, Err(ConversionError::ConversionFailure));
Add/Sub
The result of the operation is the LHS type
Examples
assert_eq!((Milliseconds(2_001_u32) - Seconds(1_u32)), Milliseconds(1_001_u32)); assert_eq!((Milliseconds(1_u32) + Seconds(1_u32)), Milliseconds(1_001_u32));
Panics
The same reason the integer operation would panic. Namely, if the result overflows the type.
Examples
let _ = Seconds(u32::MAX) + Seconds(1_u32);
Equality
assert_eq!(Seconds(123_u32), Seconds(123_u32)); assert_eq!(Seconds(123_u32), Milliseconds(123_000_u32)); assert_ne!(Seconds(123_u32), Milliseconds(123_001_u32)); assert_ne!(Milliseconds(123_001_u32), Seconds(123_u32)); assert_ne!(Milliseconds(123_001_u64), Seconds(123_u64)); assert_ne!(Seconds(123_u64), Milliseconds(123_001_u64)); assert_ne!(Seconds(123_u64), Milliseconds(123_001_u32));
Comparisons
assert!(Seconds(2_u32) < Seconds(3_u32)); assert!(Seconds(2_u32) < Milliseconds(2_001_u32)); assert!(Seconds(2_u32) == Milliseconds(2_000_u32)); assert!(Seconds(2_u32) > Milliseconds(1_999_u32)); assert!(Seconds(2_u32) < Milliseconds(2_001_u64)); assert!(Seconds(2_u64) < Milliseconds(2_001_u32));
Remainder
assert_eq!(Minutes(62_u32) % Hours(1_u32), Minutes(2_u32));
Associated Types
type Rep: TimeInt
The inner type of the Duration representing the count of the implementation unit
Associated Constants
const PERIOD: Period
A fraction/ratio representing the period of the count's LSbit. The precision of the
Duration.
Required methods
fn new(value: Self::Rep) -> Self
Not generally useful or needed as the duration can be constructed like this:
Seconds(123_u32); 123_u32.seconds();
It only exists to allow Duration methods with default definitions to create a new duration
fn count(self) -> Self::Rep
Provided methods
fn from_ticks<Rep: TimeInt>(
ticks: Rep,
period: Period
) -> Result<Self, ConversionError> where
Self::Rep: TryFrom<Rep>,
ticks: Rep,
period: Period
) -> Result<Self, ConversionError> where
Self::Rep: TryFrom<Rep>,
Constructs a Duration from a value of ticks and a period
Examples
assert_eq!(Microseconds::<u32>::from_ticks(5_u64, <Period>::new(1, 1_000)), Ok(Microseconds(5_000_u32))); // the conversion arithmetic will not cause overflow assert_eq!(Milliseconds::<u32>::from_ticks((u32::MAX as u64) + 1, <Period>::new(1, 1_000_000)), Ok(Milliseconds((((u32::MAX as u64) + 1) / 1_000) as u32)));
Errors
Failure will only occur if the value does not fit in the selected destination type.
ConversionError::Overflow : The conversion of periods causes an overflow:
assert_eq!(Milliseconds::<u32>::from_ticks(u32::MAX, <Period>::new(1, 1)), Err(ConversionError::Overflow));
ConversionError::ConversionFailure : The Self integer cast to that of the destination
type fails:
assert_eq!(Seconds::<u32>::from_ticks(u32::MAX as u64 + 1, <Period>::new(1, 1)), Err(ConversionError::ConversionFailure));
fn into_ticks<Rep: TimeInt>(
self,
period: Period
) -> Result<Rep, ConversionError> where
Self::Rep: TimeInt,
Rep: TryFrom<Self::Rep>,
self,
period: Period
) -> Result<Rep, ConversionError> where
Self::Rep: TimeInt,
Rep: TryFrom<Self::Rep>,
Create an integer representation with LSbit period of that provided
Examples
assert_eq!(Microseconds(5_000_u32).into_ticks::<u32>(<Period>::new(1, 1_000)), Ok(5_u32)); // the _into_ period can be any value assert_eq!(Microseconds(5_000_u32).into_ticks::<u32>(<Period>::new(1, 200)), Ok(1_u32)); // as long as the result fits in the provided integer, it will succeed assert_eq!(Microseconds::<u32>(u32::MAX).into_ticks::<u64>(<Period>::new(1, 2_000_000)), Ok((u32::MAX as u64) * 2));
Errors
Failure will only occur if the value does not fit in the selected destination type.
ConversionError::Overflow : The conversion of periods causes an overflow:
assert_eq!(Seconds(u32::MAX).into_ticks::<u32>(<Period>::new(1, 1_000)), Err(ConversionError::Overflow));
ConversionError::ConversionFailure : The Self integer cast to that of the destination
type fails:
assert_eq!(Seconds(u32::MAX as u64 + 1).into_ticks::<u32>(<Period>::new(1, 1)), Err(ConversionError::ConversionFailure));
#[must_use]fn min_value() -> Self::Rep
assert_eq!(u32::MIN, Seconds::<u32>::min_value());
#[must_use]fn max_value() -> Self::Rep
assert_eq!(u32::MAX, Seconds::<u32>::max_value());