我正在尝试在 Rust 中设置一个简单的计时器,该计时器以特定频率返回 true。
#[derive(Clone, Debug)]
pub struct IntervalTimer {
pub period: Duration,
pub delta: Instant,
}
impl IntervalTimer {
pub fn new(period: Duration) -> Self {
let delta = Instant::now();
Self { period, delta }
}
/// Returns true if the interval between calls has exceeded the period
pub fn ready(&mut self) -> bool {
if self.delta.elapsed() < self.period {
false
} else {
self.delta = self.delta + self.period;
true
}
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这std::time::Instant在rayon任务中使用时似乎一直很恐慌(如果重要的话,在 Legion …