From 39e1c2818944f804d68d64dae0d5639d22b0e59e Mon Sep 17 00:00:00 2001 From: FuseTim Date: Sun, 11 Jul 2021 16:09:17 +0200 Subject: [PATCH] Adding missing methods to Instant, matching Rust 1.39 --- src/wasm.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/wasm.rs b/src/wasm.rs index 57ee1e6..596b345 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -70,6 +70,25 @@ impl Instant { pub fn elapsed(&self) -> Duration { Instant::now() - *self } + + pub fn checked_duration_since(&self, earlier: Instant) -> Option { + match self.cmp(&earlier) { + Ordering::Less => None, + _ => Some(self.duration_since(earlier)), + } + } + + pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { + self.checked_duration_since(earlier).unwrap_or_default() + } + + pub fn checked_add(&self, duration: Duration) -> Option { + Some(*self + duration) + } + + pub fn checked_sub(&self, duration: Duration) -> Option { + Some(*self - duration) + } } impl Add for Instant {