Merge pull request #2295 from Frostie314159/reset-at-after

Introduce `reset_{at|after}` functions for ticker.
This commit is contained in:
Dario Nieuwenhuis 2024-03-28 19:04:26 +00:00 committed by GitHub
commit 81cf9d1143
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -190,6 +190,18 @@ impl Ticker {
self.expires_at = Instant::now() + self.duration;
}
/// Reset the ticker at the deadline.
/// If the deadline is in the past, the ticker will fire instantly.
pub fn reset_at(&mut self, deadline: Instant) {
self.expires_at = deadline + self.duration;
}
/// Resets the ticker, after the specified duration has passed.
/// If the specified duration is zero, the next tick will be after the duration of the ticker.
pub fn reset_after(&mut self, after: Duration) {
self.expires_at = Instant::now() + after + self.duration;
}
/// Waits for the next tick.
pub fn next(&mut self) -> impl Future<Output = ()> + '_ {
poll_fn(|cx| {