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

Add missing methods to Instant, matching Rust 1.39 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ impl Instant {
pub fn elapsed(&self) -> Duration {
Instant::now() - *self
}

pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
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<Instant> {
Some(*self + duration)
}

pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
Some(*self - duration)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow creating a negative Instance, right?

Instant::now() - (Instant::now() + Duration::from_secs(1))

Copy link
Author

@fusetim fusetim Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems, I don't know for sure if it should be avoided or not.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much think this should be avoided (across the whole crate). I do think we should mimic the behavior of the standard library here.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6e4ac52feea310c855e5a4d420e2d8ea

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This highly depends of the underlying implementation and could be different on an other platform. Nevertheless we could restrict the use of negative Instant.

I can't easily edit the PR for now, but I'll be back on August.

}
}

impl Add<Duration> for Instant {
Expand Down