parent
22c18205bc
commit
581f9e8089
@ -0,0 +1,22 @@ |
||||
//! 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); |
||||
} |
||||
} |
Loading…
Reference in new issue