Fix overflow panic on Stopwatch at Duration::MAX (#15927)

See #15924 for more details
close #15924

from the issue, this code panic:
```rust
use bevy::time::Stopwatch;
use std::time::Duration;

fn main() {
    let second = Duration::from_secs(1);

    let mut stopwatch = Stopwatch::new();
    // lot of time has passed... or a timer with Duration::MAX that was artificially set has "finished":
    // timer.set_elapsed(timer.remaining());
    stopwatch.set_elapsed(Duration::MAX);
    // panic
    stopwatch.tick(second);

    let mut stopwatch = Stopwatch::new();
    stopwatch.set_elapsed(Duration::MAX - second);
    // this doesnt panic as its still one off the max
    stopwatch.tick(second);
    // this panic
    stopwatch.tick(second);
}
```

with this PR changes, the code now doesn't panic.

have a good day !
This commit is contained in:
Lucas 2024-10-15 16:14:44 +02:00 committed by GitHub
parent de08fb2afa
commit af93e78b72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -129,7 +129,7 @@ impl Stopwatch {
/// ```
pub fn tick(&mut self, delta: Duration) -> &Self {
if !self.paused() {
self.elapsed += delta;
self.elapsed = self.elapsed.saturating_add(delta);
}
self
}