Crate suspend_time

Crate suspend_time 

Source
Expand description

Suspend-time is a cross-platform monotonic clock that is suspend-unaware, written in Rust! It allows system suspension (e.g. when a user closes their laptop on windows) to not affect Instant durations and timeouts!

Example of using SuspendUnawareInstant:

use std::{thread, time};
use suspend_time::{SuspendUnawareInstant};

fn main() {
    // If you used std::time::Instant here and you suspend the system on windows,
    // it will print more than 3 seconds (circa July 2024).
    // With SuspendUnawareInstant this has no effect.
    let instant = SuspendUnawareInstant::now();
    let three_secs = time::Duration::from_secs(3);
    thread::sleep(three_secs);
    println!("{:#?}", instant.elapsed());
}

Example of using suspend_time::timeout

use std::time::Duration;

#[tokio::main]
async fn main() {
    // If you suspend the system during main's execution, Tokio will time
    // out even though it only slept for 1 second. suspend_time::timeout does not.
    let _ = suspend_time::timeout(
        Duration::from_secs(2),
        suspend_time::sleep(Duration::from_secs(1)),
    ).await;
}

Structs§

SuspendUnawareInstant
Similar to the standard library’s implementation of Instant, except it is consistently unaware of system suspends across all platforms supported by this library.
TimedOutError
Suspend-time’s equivalent of tokio’s tokio::time::error::Elapsed. Constructing the Elapsed struct is impossible due to its private construct and private members. As such, we must create our own struct

Functions§

sleep
The same API as tokio::time::sleep, except it is uses on SuspendUnawareInstant for measuring time.
timeout
The same API as tokio::time::timeout, except it is uses on SuspendUnawareInstant for measuring time.