Struct Instant

Source
pub struct Instant<Clock: Clock> { /* private fields */ }
Expand description

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.try_now().unwrap();

However, an Instant can also be constructed directly. In this case the constructed Instant is 23 * SomeClock::SCALING_FACTOR seconds since the clock’s epoch

Instant::<SomeClock>::new(23);

Implementations§

Source§

impl<Clock: Clock> Instant<Clock>

Source

pub fn new(ticks: Clock::T) -> Self

Construct a new Instant from the provided Clock

Source

pub fn checked_duration_since(&self, other: &Self) -> Option<Generic<Clock::T>>

Returns the amount of time elapsed from another instant to this one as a duration::Generic or None if the other instant is later than this one.

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

// Given `instant1` at 3 `Clock` ticks
// Given `instant2` at 5 `Clock` ticks
let generic_duration = instant2.checked_duration_since(&instant1).unwrap();

// Convert into a _named_ `Duration`
let microseconds: Microseconds<u32> = generic_duration.try_into().unwrap();

assert_eq!(microseconds, Microseconds(2_000_u32));
Source

pub fn checked_duration_until(&self, other: &Self) -> Option<Generic<Clock::T>>

Returns the amount of time elapsed from self until that given instant duration::Generic or None if the other instant is later than this one.

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

// Given `instant1` at 3 `Clock` ticks
// Given `instant2` at 5 `Clock` ticks
let generic_duration = instant1.checked_duration_until(&instant2).unwrap();

// Convert into a _named_ `Duration`
let microseconds: Microseconds<u32> = generic_duration.try_into().unwrap();

assert_eq!(microseconds, Microseconds(2_000_u32));
Source

pub fn duration_since_epoch(&self) -> Generic<Clock::T>

Returns the Duration (in the provided units) since the beginning of time (the Clock’s 0)

Source

pub fn checked_add<Dur>(self, duration: Dur) -> Option<Self>
where Dur: FixedPoint + Duration, Clock::T: TryFrom<Dur::T> + Div<Output = Clock::T>,

This Instant + Duration = later (future) Instant

Returns None if the Duration is too large

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

assert_eq!(
    Instant::<Clock>::new(0).checked_add(Milliseconds(u32::MAX/2)),
    Some(Instant::<Clock>::new(u32::MAX/2))
);

assert_eq!(
    Instant::<Clock>::new(0).checked_add(Milliseconds(u32::MAX/2 + 1)),
    None
);
Source

pub fn checked_sub<Dur>(self, duration: Dur) -> Option<Self>
where Dur: FixedPoint + Duration, Clock::T: TryFrom<Dur::T> + Div<Output = Clock::T>,

This Instant - Duration = earlier Instant

Returns None if the Duration is too large

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

assert_eq!(Instant::<Clock>::new(u32::MAX).checked_sub(Milliseconds(u32::MAX/2)),
    Some(Instant::<Clock>::new(u32::MAX - u32::MAX/2)));

assert_eq!(Instant::<Clock>::new(u32::MAX).checked_sub(Milliseconds(u32::MAX/2 + 1)),
    None);

Trait Implementations§

Source§

impl<Clock: Clock, Dur> Add<Dur> for Instant<Clock>
where Clock::T: TryFrom<Dur::T>, Dur: FixedPoint + Duration,

Source§

fn add(self, rhs: Dur) -> Self::Output

Add a Duration to an Instant resulting in a new, later Instant

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::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 T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

Instant::<Clock>::new(0) + Milliseconds(u32::MAX/2 + 1);
Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Hours<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Microseconds<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Milliseconds<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Minutes<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Nanoseconds<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: TimeInt, Clock: Clock> Add<Instant<Clock>> for Seconds<T>
where Clock::T: TryFrom<T>,

Source§

type Output = Instant<Clock>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Instant<Clock>) -> Self::Output

Performs the + operation. Read more
Source§

impl<Clock: Clock> Clone for Instant<Clock>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Clock: Debug + Clock> Debug for Instant<Clock>
where Clock::T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Clock: Clock> Hash for Instant<Clock>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Clock> Ord for Instant<Clock>
where Clock: Clock, Clock::T: Div<Output = Clock::T>,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<Clock: Clock> PartialEq for Instant<Clock>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Clock: Clock> PartialOrd for Instant<Clock>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Calculates the difference between two Instants resulting in a Duration

struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::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));
assert!(Instant::<Clock>::new(5) <= Instant::<Clock>::new(5));
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<Clock: Clock, Dur> Sub<Dur> for Instant<Clock>
where Clock::T: TryFrom<Dur::T>, Dur: FixedPoint + Duration,

Source§

fn sub(self, rhs: Dur) -> Self::Output

Subtract a Duration from an Instant resulting in a new, earlier Instant

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::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 T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

Instant::<Clock>::new(u32::MAX) - Milliseconds(u32::MAX/2 + 1);
Source§

type Output = Instant<Clock>

The resulting type after applying the - operator.
Source§

impl<Clock: Clock> Sub for Instant<Clock>

Source§

fn sub(self, rhs: Instant<Clock>) -> Self::Output

Subtract a two Instants resulting in a Duration

§Examples
struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

assert_eq!((Instant::<Clock>::new(5_001) - Instant::<Clock>::new(5_000)).integer(), 1);
§Panics

Virtually the same reason the integer operation would panic. Namely, if the result overflows the type. Specifically, if the right hand side Instant is larger than the left hand side.

struct Clock;
impl embedded_time::Clock for Clock {
    type T = u32;
    const SCALING_FACTOR: Fraction = Fraction::new(1, 1_000);
    // ...
}

Instant::<Clock>::new(0) - Instant::<Clock>::new(1);
Source§

type Output = Generic<<Clock as Clock>::T>

The resulting type after applying the - operator.
Source§

impl<Clock: Clock> Copy for Instant<Clock>

Source§

impl<Clock: Clock> Eq for Instant<Clock>

Auto Trait Implementations§

§

impl<Clock> Freeze for Instant<Clock>
where <Clock as Clock>::T: Freeze,

§

impl<Clock> RefUnwindSafe for Instant<Clock>
where <Clock as Clock>::T: RefUnwindSafe,

§

impl<Clock> Send for Instant<Clock>
where <Clock as Clock>::T: Send,

§

impl<Clock> Sync for Instant<Clock>
where <Clock as Clock>::T: Sync,

§

impl<Clock> Unpin for Instant<Clock>
where <Clock as Clock>::T: Unpin,

§

impl<Clock> UnwindSafe for Instant<Clock>
where <Clock as Clock>::T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.