Fixed internal overflow on subtraiton in elapsed and duration

This commit is contained in:
Per Lindgren 2019-11-18 16:36:17 +01:00
parent 725d5e1aa9
commit dfab15ed78
2 changed files with 9 additions and 2 deletions

View file

@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
## v0.5.1 - 2019-11-18
- Fixed arithmetic wrapping bug in src/cyccntr.rs
elapsed and duration could cause an internal overflow trap
on subtraction in debug mode.
## v0.5.0 - 2019-11-14
### Added

View file

@ -38,12 +38,14 @@ impl Instant {
/// Returns the amount of time elapsed since this instant was created.
pub fn elapsed(&self) -> Duration {
Instant::now() - *self
let diff = Instant::now().inner.wrapping_sub(self.inner);
assert!(diff >= 0, "instant now is earlier than self");
Duration { inner: diff as u32 }
}
/// Returns the amount of time elapsed from another instant to this one.
pub fn duration_since(&self, earlier: Instant) -> Duration {
let diff = self.inner - earlier.inner;
let diff = self.inner.wrapping_sub(earlier.inner);
assert!(diff >= 0, "second instant is later than self");
Duration { inner: diff as u32 }
}