[][src]Struct embedded_time::instant::Instant

pub struct Instant<Clock> where
    Clock: Clock
{ /* fields omitted */ }

Represents an instant of time relative to a specific clock

Example

Create an Instant that is 23 * SomeClock::PERIOD seconds since the clock's epoch:

This example is not tested
Instant::<SomeClock>::new(23);

Implementations

impl<Clock> Instant<Clock> where
    Clock: Clock
[src]

pub fn new(ticks: Clock::Rep) -> Self[src]

pub fn duration_since<Dur>(&self, other: &Self) -> Option<Dur> where
    Dur: Duration,
    Dur::Rep: TryFrom<Clock::Rep>, 
[src]

Calculates the difference between two Instances resulting in a Duration

Examples

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(1, 1_000);
    // ...
}

let diff: Option<Milliseconds<_>> = Instant::<Clock>::new(5).duration_since(&Instant::<Clock>::new(3));
assert_eq!(diff, Some(Milliseconds(2_i32)));

let diff: Option<Microseconds<i64>> = Instant::<Clock>::new(5).duration_since(&Instant::<Clock>::new(3));
assert_eq!(diff, Some(Microseconds(2_000_i64)));

let diff: Option<Microseconds<i64>> = Instant::<Clock>::new(i32::MIN).duration_since(&Instant::<Clock>::new(i32::MAX));
assert_eq!(diff, Some(Microseconds(1_000_i64)));

let diff: Option<Seconds<i64>> = Instant::<Clock>::new(1_000).duration_since(&Instant::<Clock>::new(-1_000));
assert_eq!(diff, Some(Seconds(2_i64)));

pub fn elapsed_since_epoch<Dur>(&self) -> Option<Dur> where
    Dur: Duration,
    Dur::Rep: TryFrom<Clock::Rep>,
    Clock::Rep: From<i32>, 
[src]

Trait Implementations

impl<Clock, Dur> Add<Dur> for Instant<Clock> where
    Clock: Clock,
    Clock::Rep: TryFrom<Dur::Rep>,
    Dur: Duration
[src]

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 instance

Panics

If Duration::into_ticks() returns None. In this case, i32::MAX of seconds cannot be converted to the clock precision of milliseconds with i32 storage.

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(1, 1_000);
    // ...
}

Instant::<Clock>::new(1) + Seconds(i32::MAX);

See also: impl Add for Duration

Examples

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(1, 1_000);
    // ...
}

assert_eq!(Instant::<Clock>::new(1) + Seconds(3), Instant::<Clock>::new(3_001));
assert_eq!(Instant::<Clock>::new(-1) + Milliseconds(5_123), Instant::<Clock>::new(5_122));
assert_eq!(Instant::<Clock>::new(1) + Milliseconds(700), Instant::<Clock>::new(701));
assert_eq!(Instant::<Clock>::new(1_i32) + Milliseconds(700_i64), Instant::<Clock>::new(701_i32));

impl<Clock> Clone for Instant<Clock> where
    Clock: Clock
[src]

impl<Clock> Copy for Instant<Clock> where
    Clock: Clock
[src]

impl<Clock: Debug> Debug for Instant<Clock> where
    Clock: Clock,
    Clock::Rep: Debug
[src]

impl<Clock> Eq for Instant<Clock> where
    Clock: Clock
[src]

impl<Clock> Ord for Instant<Clock> where
    Clock: Clock
[src]

impl<Clock> PartialEq<Instant<Clock>> for Instant<Clock> where
    Clock: Clock
[src]

impl<Clock> PartialOrd<Instant<Clock>> for Instant<Clock> where
    Clock: Clock
[src]

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

Calculates the difference between two Instances resulting in a Duration

Examples

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(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(i32::MAX) < Instant::<Clock>::new(i32::MIN));

impl<Clock, Dur> Sub<Dur> for Instant<Clock> where
    Clock: Clock,
    Clock::Rep: TryFrom<Dur::Rep>,
    Dur: Duration
[src]

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 instance

Panics

If Duration::into_ticks() returns None. In this case, i32::MAX of seconds cannot be converted to the clock precision of milliseconds with i32 storage.

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(1, 1_000);
    // ...
}

Instant::<Clock>::new(1) - Seconds(i32::MAX);

See also impl Sub for Duration

Examples

struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = i32;
    const PERIOD: Period = Period::new_raw(1, 1_000);
    // ...
}

assert_eq!(Instant::<Clock>::new(1) - Seconds(3), Instant::<Clock>::new(-2_999));
assert_eq!(Instant::<Clock>::new(-1) - Milliseconds(5_123), Instant::<Clock>::new(-5_124));
assert_eq!(Instant::<Clock>::new(800) - Milliseconds(700), Instant::<Clock>::new(100));
assert_eq!(Instant::<Clock>::new(5_000_i32) - Milliseconds(700_i64), Instant::<Clock>::new(4_300_i32));

Auto Trait Implementations

impl<Clock> Send for Instant<Clock> where
    <Clock as Clock>::Rep: Send

impl<Clock> Sync for Instant<Clock> where
    <Clock as Clock>::Rep: Sync

impl<Clock> Unpin for Instant<Clock> where
    <Clock as Clock>::Rep: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.