[−][src]Struct embedded_time::Instant
Represents an instant of time relative to a specific Clock
Example
Typically an Instant
will be obtained from a Clock
let some_clock = SomeClock; let some_instant = some_clock.now().unwrap();
However, an Instant
can also be constructed directly. In this case the constructed Instant
is 23 * SomeClock::PERIOD
seconds since the clock's epoch
Instant::<SomeClock>::new(23);
Implementations
impl<Clock: Clock> Instant<Clock>
[src]
pub fn new(ticks: Clock::Rep) -> Self
[src]
Construct a new Instant from the provided Clock
pub fn duration_since<Dur: Duration>(
&self,
other: &Self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
[src]
&self,
other: &Self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
Returns the Duration
since the given Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Ok(Microseconds(2_000_u64)), Instant::<Clock>::new(5).duration_since::<Microseconds<u64>>(&Instant::<Clock>::new(3))); assert_eq!(Err(ConversionError::NegDuration), Instant::<Clock>::new(3).duration_since::<Microseconds<u64>>(&Instant::<Clock>::new(5)));
Errors
ConversionError::NegDuration
:Instant
is in the futureConversionError::Overflow
: problem coverting to the desiredDuration
ConversionError::ConversionFailure
: problem coverting to the desiredDuration
pub fn duration_until<Dur: Duration>(
&self,
other: &Self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
[src]
&self,
other: &Self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
Returns the Duration
until the given Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(5).duration_until::<Microseconds<u64>>(&Instant::<Clock>::new(7)), Ok(Microseconds(2_000_u64))); assert_eq!(Instant::<Clock>::new(7).duration_until::<Microseconds<u64>>(&Instant::<Clock>::new(5)), Err(ConversionError::NegDuration));
Errors
ConversionError::NegDuration
:Instant
is in the pastConversionError::Overflow
: problem coverting to the desiredDuration
ConversionError::ConversionFailure
: problem coverting to the desiredDuration
pub fn duration_since_epoch<Dur: Duration>(
&self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
Clock::Rep: TryFrom<Dur::Rep>,
[src]
&self
) -> Result<Dur, ConversionError> where
Dur::Rep: TryFrom<Clock::Rep>,
Clock::Rep: TryFrom<Dur::Rep>,
Returns the Duration
(in the provided units) since the beginning of time (the
Clock
's 0)
If it is a wrapping clock, the result is meaningless.
pub fn checked_add_duration<Dur: Duration>(
self,
duration: Dur
) -> Result<Self, ConversionError> where
Clock::Rep: TryFrom<Dur::Rep>,
[src]
self,
duration: Dur
) -> Result<Self, ConversionError> where
Clock::Rep: TryFrom<Dur::Rep>,
Subtract a Duration
from an Instant
resulting in a new, earlier Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700_u32), Instant::<Clock>::new(100)); assert_eq!(Instant::<Clock>::new(5_000) - Milliseconds(700_u64), Instant::<Clock>::new(4_300)); // maximum duration allowed assert_eq!(Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32), Instant::<Clock>::new(u32::MAX/2 + 1));
Errors
ConversionError::Overflow
: The duration is more than half the wrap-around period of the
clock
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(0).checked_add_duration(Milliseconds(u32::MAX/2 + 1)), Err(ConversionError::Overflow));
pub fn checked_sub_duration<Dur: Duration>(
self,
duration: Dur
) -> Result<Self, ConversionError> where
Clock::Rep: TryFrom<Dur::Rep>,
[src]
self,
duration: Dur
) -> Result<Self, ConversionError> where
Clock::Rep: TryFrom<Dur::Rep>,
Adds a Duration
to an Instant
resulting in a new, later Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700_u32), Instant::<Clock>::new(100)); assert_eq!(Instant::<Clock>::new(5_000) - Milliseconds(700_u64), Instant::<Clock>::new(4_300)); // maximum duration allowed assert_eq!(Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32), Instant::<Clock>::new(u32::MAX/2 + 1));
Errors
ConversionError::Overflow
: The duration is more than half the wrap-around period of the
clock
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(u32::MAX).checked_sub_duration(Milliseconds(u32::MAX/2 + 1)), Err(ConversionError::Overflow));
Trait Implementations
impl<Clock: Clock, Dur: Duration> Add<Dur> for Instant<Clock> where
Clock::Rep: TryFrom<Dur::Rep>,
[src]
Clock::Rep: TryFrom<Dur::Rep>,
Add a Duration
to an Instant
resulting in a later Instant
type Output = Self
The resulting type after applying the +
operator.
fn add(self, rhs: Dur) -> Self::Output
[src]
Add a Duration
to an Instant
resulting in a new, later Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(1) + Seconds(3_u32), Instant::<Clock>::new(3_001)); assert_eq!(Instant::<Clock>::new(1) + Milliseconds(700_u32), Instant::<Clock>::new(701)); assert_eq!(Instant::<Clock>::new(1) + Milliseconds(700_u64), Instant::<Clock>::new(701)); // maximum duration allowed assert_eq!(Instant::<Clock>::new(0) + Milliseconds(i32::MAX as u32), Instant::<Clock>::new(u32::MAX/2));
Panics
Virtually the same reason the integer operation would panic. Namely, if the result overflows the type. Specifically, if the duration is more than half the wrap-around period of the clock.
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } Instant::<Clock>::new(0) + Milliseconds(u32::MAX/2 + 1);
impl<Clock: Clock> Clone for Instant<Clock>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<Clock: Clock> Copy for Instant<Clock>
[src]
impl<Clock: Debug + Clock> Debug for Instant<Clock> where
Clock::Rep: Debug,
[src]
Clock::Rep: Debug,
impl<Clock: Clock> Eq for Instant<Clock>
[src]
impl<Clock: Clock> Ord for Instant<Clock>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<Clock: Clock> PartialEq<Instant<Clock>> for Instant<Clock>
[src]
impl<Clock: Clock> PartialOrd<Instant<Clock>> for Instant<Clock>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
Calculates the difference between two Instance
s resulting in a Duration
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert!(Instant::<Clock>::new(5) > Instant::<Clock>::new(3)); assert!(Instant::<Clock>::new(5) == Instant::<Clock>::new(5)); assert!(Instant::<Clock>::new(u32::MAX) < Instant::<Clock>::new(u32::MIN));
#[must_use]fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<Clock: Clock, Dur: Duration> Sub<Dur> for Instant<Clock> where
Clock::Rep: TryFrom<Dur::Rep>,
[src]
Clock::Rep: TryFrom<Dur::Rep>,
Subtract a Duration
from an Instant
resulting in an earlier Instant
type Output = Self
The resulting type after applying the -
operator.
fn sub(self, rhs: Dur) -> Self::Output
[src]
Subtract a Duration
from an Instant
resulting in a new, earlier Instant
Examples
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } assert_eq!(Instant::<Clock>::new(5_001) - Seconds(3_u32), Instant::<Clock>::new(2_001)); assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700_u32), Instant::<Clock>::new(100)); assert_eq!(Instant::<Clock>::new(5_000) - Milliseconds(700_u64), Instant::<Clock>::new(4_300)); // maximum duration allowed assert_eq!(Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32), Instant::<Clock>::new(u32::MAX/2 + 1));
Panics
Virtually the same reason the integer operation would panic. Namely, if the result overflows the type. Specifically, if the duration is more than half the wrap-around period of the clock.
struct Clock; impl embedded_time::Clock for Clock { type Rep = u32; const PERIOD: Period = <Period>::new(1, 1_000); // ... } Instant::<Clock>::new(u32::MAX) - Milliseconds(u32::MAX/2 + 1);
Auto Trait Implementations
impl<Clock> RefUnwindSafe for Instant<Clock> where
<Clock as Clock>::Rep: RefUnwindSafe,
<Clock as Clock>::Rep: RefUnwindSafe,
impl<Clock> Send for Instant<Clock> where
<Clock as Clock>::Rep: Send,
<Clock as Clock>::Rep: Send,
impl<Clock> Sync for Instant<Clock> where
<Clock as Clock>::Rep: Sync,
<Clock as Clock>::Rep: Sync,
impl<Clock> Unpin for Instant<Clock> where
<Clock as Clock>::Rep: Unpin,
<Clock as Clock>::Rep: Unpin,
impl<Clock> UnwindSafe for Instant<Clock> where
<Clock as Clock>::Rep: UnwindSafe,
<Clock as Clock>::Rep: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,