You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
534 B
22 lines
534 B
//! A timed loop implementation
|
|
|
|
use async_std::{future::Future, task};
|
|
use chrono::{DateTime, Utc};
|
|
use std::{thread, time::Duration};
|
|
|
|
/// Number of ticks per second
|
|
const TICKS: u64 = 100;
|
|
const TICK_TIME: Duration = Duration::from_millis(1000 / TICKS);
|
|
|
|
pub fn block_loop<F>(f: F)
|
|
where
|
|
F: Future<Output = ()> + Send + Copy + 'static,
|
|
{
|
|
loop {
|
|
let t1 = Utc::now();
|
|
task::block_on(f);
|
|
let t2 = Utc::now();
|
|
let t3 = (t2 - t1).to_std().unwrap();
|
|
thread::sleep(TICK_TIME - t3);
|
|
}
|
|
}
|
|
|