Skip to main content

InstantContext

Trait InstantContext 

Source
pub trait InstantContext: Sized {
    // Required methods
    fn checked_add_wc(&self, duration: Duration) -> Result<Self>;
    fn checked_sub_wc(&self, duration: Duration) -> Result<Self>;
    fn checked_duration_since_wc(&self, earlier: Instant) -> Result<Duration>;
}

Required Methods§

Source

fn checked_add_wc(&self, duration: Duration) -> Result<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.

Source

fn checked_sub_wc(&self, duration: Duration) -> Result<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.

Source

fn checked_duration_since_wc(&self, earlier: Instant) -> Result<Duration>

Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one.

Due to monotonicity bugs, even under correct logical ordering of the passed Instants, this method can return None.

§Examples
use std::time::{Duration, Instant};
use std::thread::sleep;
 
let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.checked_duration_since(now));
println!("{:?}", now.checked_duration_since(new_now)); // None

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl InstantContext for Instant

Source§

fn checked_add_wc(&self, duration: Duration) -> Result<Self>

Source§

fn checked_sub_wc(&self, duration: Duration) -> Result<Self>

Source§

fn checked_duration_since_wc(&self, earlier: Instant) -> Result<Duration>

Implementors§