#sleep #control-flow #accuracy #intermittent

isleep

Intermittent sleeping and return the control flow inbetween

6 releases

0.3.2 Sep 13, 2023
0.3.1 Sep 13, 2023
0.2.1 Sep 13, 2023
0.1.0 Sep 3, 2023

#3 in #accuracy

Download history 37/week @ 2024-02-26 1/week @ 2024-03-11 90/week @ 2024-04-01

91 downloads per month

Apache-2.0

7KB

isleep

Provides the functions snooze and accurate_snooze for intermittent sleeping which return the control flow inbetween.

Relies on platform support for std::time and std::thread.

Example

// Sleeping for a total of 1 s
let total = std::time::Duration::from_secs(1);
// Interrupting the sleep after 100 ms
let len = std::time::Duration::from_millis(100);
// Starting now
let start = std::time::Instant::now();
// Sleeps for `total` in steps up to `len`
// Wont sleep longer than `total` within accuracy of the platform
while snooze(start, total, len) {
   println!("Checking if the user pressed CTRL+C...");
}

Accuracy

The accuracy is platform dependent and might be low for small durations (eg: <20 ms on Windows). Higher accuracy can be achieved with the accuracy feature and accurate_snooze which utilizes spin_sleep.

cargo add isleep --features=accuracy


lib.rs:

Provides the functions snooze and accurate_snooze for intermittent sleeping which return the control flow inbetween.

Relies on platform support for std::time and std::thread.

Example

// Sleeping for a total of 1 s
let total = std::time::Duration::from_secs(1);
// Interrupting the sleep after 100 ms
let len = std::time::Duration::from_millis(100);
// Starting now
let start = std::time::Instant::now();
// Sleeps for `total` in steps up to `len`
// Wont sleep longer than `total` within accuracy of the platform
while isleep::snooze(start, total, len) {
   println!("Checking if the user pressed CTRL+C...");
}

Accuracy

The accuracy is platform dependent and might be low for small durations (eg: <20 ms on Windows). Higher accuracy can be achieved with the accuracy feature and accurate_snooze which utilizes spin_sleep.

cargo add isleep --features=accuracy

Dependencies

~0–8MB
~43K SLoC