I am in Rust using tokio.

I know I can set up a tokio loop to wake every half second with

let period = std::time::Duration::from_secs_f32(0.5);
let mut interval = tokio::time::interval(period);

loop {
tokio::select! {
_ = interval.tick() => {}

And I know I can reset() the loop to say "forget what schedule you were on".

Is there a way to do this, but have the interval wake ONLY WHEN IT WAS RESET? i.e. it fires half a second after reset() but at no other time

Hmm, replacing tokio:: Interval with tokio::Sleep looks promising, but now I have a new problem: If I wake up early, and then decide I want to "cancel the alarm", there's no way to do this.

I wonder if I could Either<> with a Sleep and some kind of special Rust async that never ever returns. I wonder if that would create perf issues because it would mean repeatedly creating and destroying Sleeps

Follow

@mcc You have that problem if you wake up on time, too (expired sleep will remain expired forever). Hacky solution: reset it to some time in far future.

But your setup with Either need not require creating and destroying the future all the time: you can make an `Either<&Sleep, SomethingElse>` (well, remake it each iteration).

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.