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

wasm::Instant: Add {checked,saturating}_duration_since #36

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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