pub trait Instant:
Sized
+ Add<Duration, Output = Self>
+ AddAssign<Duration>
+ Clone
+ Copy
+ Debug
+ Eq
+ Hash
+ Ord
+ PartialEq<Self>
+ PartialOrd<Self>
+ Sub<Duration, Output = Self>
+ Sub<Self, Output = Duration>
+ SubAssign<Duration>
+ RefUnwindSafe
+ Send
+ Sync
+ Unpin
+ UnwindSafe
+ Any
+ Borrow<Self>
+ BorrowMut<Self>
+ From<Self> {
// Required methods
fn checked_duration_since(&self, earlier: Self) -> Option<Duration>;
fn checked_add(&self, duration: Duration) -> Option<Self>;
fn checked_sub(&self, duration: Duration) -> Option<Self>;
// Provided methods
fn duration_since(&self, earlier: Self) -> Duration { ... }
fn saturating_duration_since(&self, earlier: Self) -> Duration { ... }
}Expand description
Trait for types which represent measurements of monotonically nondecreasing clocks
This is heavily inspired by the Rust stdlib’s
std::time::Instant type and
tries to replicate its behaviour wherever possible. In contrast to std::time::Instant which
relies on a globally available clock, this trait only describes the behavior which is available
without a global clock. Everything which requires a global clock is moved to the
Clock trait.
To be implemented on a given type, this trait requires that type to implement the same traits
as std::time::Instant. This might look like a huge constraint for implementers at first,
but it actually gives more possibilities to users. And it seems reasonable to require all these
traits because the underlying data structure will most probably just be a struct containing
some kind of numbers (which should not be NaN).
Required Methods§
Sourcefn checked_duration_since(&self, earlier: Self) -> Option<Duration>
fn checked_duration_since(&self, earlier: Self) -> Option<Duration>
Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one.
§Examples
use core::time::Duration;
use embedded_timers::instant::Instant;
use embedded_timers::clock::Clock;
let clock = MyClock;
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.now();
println!("{:?}", new_now.checked_duration_since(now));
println!("{:?}", now.checked_duration_since(new_now)); // NoneSourcefn checked_add(&self, duration: Duration) -> Option<Self>
fn checked_add(&self, duration: Duration) -> Option<Self>
Returns Some(t) where t is the time self + duration if t can be represented as
Self (which means it’s inside the bounds of the underlying data structure), None
otherwise.
Sourcefn checked_sub(&self, duration: Duration) -> Option<Self>
fn checked_sub(&self, duration: Duration) -> Option<Self>
Returns Some(t) where t is the time self - duration if t can be represented as
Instant (which means it’s inside the bounds of the underlying data structure), None
otherwise.
Provided Methods§
Sourcefn duration_since(&self, earlier: Self) -> Duration
fn duration_since(&self, earlier: Self) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
§Panics
As in the current version of the Rust stdlib, this method saturates instead of panicking.
§Examples
use core::time::Duration;
use embedded_timers::instant::Instant;
use embedded_timers::clock::Clock;
let clock = MyClock;
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.now();
println!("{:?}", new_now.duration_since(now));
println!("{:?}", now.duration_since(new_now)); // 0nsSourcefn saturating_duration_since(&self, earlier: Self) -> Duration
fn saturating_duration_since(&self, earlier: Self) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
§Examples
use core::time::Duration;
use embedded_timers::instant::Instant;
use embedded_timers::clock::Clock;
let clock = MyClock;
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.now();
println!("{:?}", new_now.saturating_duration_since(now));
println!("{:?}", now.saturating_duration_since(new_now)); // 0nsDyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.