Function swanling::util::sleep_minus_drift [−][src]
pub async fn sleep_minus_drift(duration: Duration, drift: Instant) -> Instant
Expand description
Sleep for a specified duration, minus the time spent doing other things.
Example
use swanling::util; async fn loop_with_delay() { loop { // Start drift timer. let mut drift_timer = tokio::time::Instant::now(); // Do other stuff, in this case sleep 250 milliseconds. This is // the "drift" that will be subtracted from the sleep time later. tokio::time::sleep(tokio::time::Duration::from_millis(250)); // Sleep for 1 second minus the time spent doing other stuff. drift_timer = util::sleep_minus_drift( tokio::time::Duration::from_secs(1), drift_timer, ).await; // Normally the loop would continue, and the amount of time doing // other things would vary each time, but the total time to complete // the loop would remain the same. break; } }