Open
Conversation
This wasn't very Rust friendly by holding on to a callback that was used to add events whenever the low threshold was crossed. This was more or less a port of the C++ version of this component. With this change, the caller will be notified (by return value) if the threshold was crossed downwards, so that it can add events themselves. This removes the callback, the lifetime modifier, and having to clone events.
Just minor improvements to use Rust language features.
lndmrk
approved these changes
Mar 20, 2026
| impl AddAssign<usize> for ThresholdWatcher<'_> { | ||
| fn add_assign(&mut self, rhs: usize) { | ||
| self.value += rhs; | ||
| // Returns `true` if the threshold was crossed downwards. |
Collaborator
There was a problem hiding this comment.
/// To make it a doc comment (since it's pub)
| (self.low_cb)(); | ||
| } | ||
| self.low_threshold = low_threshold; | ||
| pub fn add(&mut self, amount: usize) { |
Collaborator
There was a problem hiding this comment.
Maybe add a comment here for consistency
| if old_value > self.low_threshold && self.value <= self.low_threshold { | ||
| (self.low_cb)(); | ||
| } | ||
| // Returns `true` if the new threshold is lower than the current value, which means that the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The large part of this PR is to change ThresholdWatcher to be a "simpler component" by not having it trigger callbacks itself. It was more or less ported from C++ and used a C++ way of doing things.
Now it is more explicit, and requires callers to perform actions when the threshold is crossed (which the mutators indicate). This removes the lifetime annotations and having to clone the events, and all that.
And then there are some very minor refactorings to use the Rust language better.