Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clock: ensure that updates happen on unit boundaries #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Qelxiros
Copy link

The Problem

00:00:00 ----- A ------ 00:01:00 ----- B ------ 00:02:00

In the above diagram, the times are minute boundaries. If the bar is started at A, it will check the time and correctly display 00:00 (assuming the format string %H:%M). However, it will then sleep for (just under) a minute, and it won't display 00:01 until point B. If point A is closer to the end of its minute, the clock could be almost a full minute off.

The Solution

First, I added a generic to the Clock struct, allowing for different levels of Precision. There are currently four options: Days, Hours, Minutes, and Seconds. I replaced the clock widget's IntervalStream with a custom ClockStream. It also holds an internal Interval, but when the interval completes, the Stream updates the Interval's deadline to the next boundary of the Clock's precision. It's impossible to use the same generic on the ClockStream struct because we need distinct implementations of poll_next(), and we can't prove to Rust that we've implemented Stream exhaustively without something like impl<P: Precision> Stream for ClockStream<P>. The solution is to separate the part of that implementation and pass it to the ClockStream constructor in the form of an anonymous function.

Notes

I tried many options before arriving at this one, so my explanation might not be clear. Please let me know if you have any questions!
I did have to bump tokio to version 1.30.0 to gain access to the reset_at() function on Interval. I'm not sure what standard practice is here, but any version of tokio between 1.30.0 and 1.37.0 (the latest version as I write this) should work. I'm happy to change the Cargo.toml if necessary.

  • clock: ensure that updates happen on unit boundaries
  • clock: update documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant