[][src]Struct embedded_time::Instant

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

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 with ticks of the provided Clock.

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

Returns the Duration since the given Instant

Errors

  • (): RHS is a future Instant
  • (): problem coverting to desired Duration

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_since::<Microseconds<u64>>(&Instant::<Clock>::new(3)),
    Ok(Microseconds(2_000_u64)));

assert_eq!(Instant::<Clock>::new(3).duration_since::<Microseconds<u64>>(&Instant::<Clock>::new(5)),
    Err(()));

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

Returns the Duration until the given Instant

Errors

  • (): RHS is a past Instant
  • (): problem coverting to desired Duration

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(()));

pub fn duration_since_epoch<Dur: Duration>(&self) -> Result<Dur, ()> where
    Dur::Rep: TryFrom<Clock::Rep>,
    Clock::Rep: TryFrom<Dur::Rep>, 
[src]

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

Trait Implementations

impl<Clock: Clock, Dur: Duration> Add<Dur> for Instant<Clock> where
    Clock::Rep: TryFrom<Dur::Rep>, 
[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, u32::MAX of seconds cannot be converted to the clock precision of milliseconds with u32 storage.

This example panics
struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = u32;
    const PERIOD: Period =<Period>::new(1, 1_000);
    // ...
}

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

If the the Duration is greater than the signed Clock::Rep max value which would cause the result to (logically) overflow

This example panics
struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = u32;
    const PERIOD: Period =<Period>::new(1, 1_000);
    // ...
}

let _ = Instant::<Clock>::new(0) + Milliseconds(i32::MAX as u32 + 1);

See also impl Sub for Duration

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));

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

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

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

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

impl<Clock: Clock> Ord for Instant<Clock>[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 Instances 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));

impl<Clock: Clock, Dur: Duration> Sub<Dur> for Instant<Clock> where
    Clock::Rep: TryFrom<Dur::Rep>, 
[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, u32::MAX of seconds cannot be converted to the clock precision of milliseconds with u32 storage.

This example panics
struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = u32;
    const PERIOD: Period =<Period>::new(1, 1_000);
    // ...
}

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

If the the Duration is greater than the signed Clock::Rep max value which would cause the result to (logically) underflow

This example panics
struct Clock;
impl embedded_time::Clock for Clock {
    type Rep = u32;
    const PERIOD: Period = <Period>::new(1, 1_000);
    // ...
}

let _ = Instant::<Clock>::new(u32::MAX) - Milliseconds(i32::MAX as u32 + 1);

See also impl Sub for Duration

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));

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.