Skip to content

Commit

Permalink
wasm::Instant: Add {checked,saturating}_duration_since
Browse files Browse the repository at this point in the history
This brings the provided API on par with `std::time::Instant`.
  • Loading branch information
wngr authored and sebcrozet committed Aug 28, 2021
1 parent 1f72ffd commit 0e0de0f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ impl Instant {
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
self.0.checked_sub(duration).map(Instant)
}

/// Returns the amount of time elapsed from another instant to this one, or None if that
/// instant is later than this one.
#[inline]
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
if earlier.0 > self.0 {
None
} else {
Some(self.0 - earlier.0)
}
}

/// Returns the amount of time elapsed from another instant to this one, or zero duration if
/// that instant is later than this one.
#[inline]
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.checked_duration_since(earlier).unwrap_or_default()
}
}

impl Add<Duration> for Instant {
Expand Down

0 comments on commit 0e0de0f

Please sign in to comment.