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

Instant::checked_duration_since #58395

Merged
merged 3 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
#![feature(non_exhaustive)]
#![feature(alloc_layout_extra)]
#![feature(maybe_uninit)]
#![feature(checked_duration_since)]
#![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
feature(global_asm, range_contains, slice_index_methods,
decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))]
Expand Down
60 changes: 60 additions & 0 deletions src/libstd/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,52 @@ impl Instant {
self.0.sub_instant(&earlier.0)
}

/// Returns the amount of time elapsed from another instant to this one,
/// or None if that instant is earlier than this one.
///
/// # Examples
///
/// ```no_run
/// #![feature(checked_duration_since)]
/// use std::time::{Duration, Instant};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// use std::time::{Duration, Instant};
/// #![feature(checked_duration_since)]
/// use std::time::{Duration, Instant};

/// use std::thread::sleep;
///
/// let now = Instant::now();
/// sleep(Duration::new(1, 0));
/// let new_now = Instant::now();
/// println!("{:?}", new_now.checked_duration_since(now));
/// println!("{:?}", now.checked_duration_since(new_now)); // None
/// ```
#[unstable(feature = "checked_duration_since", issue = "58402")]
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
if self >= &earlier {
Some(self.0.sub_instant(&earlier.0))
} else {
None
}
}

/// Returns the amount of time elapsed from another instant to this one,
/// or zero duration if that instant is earlier than this one.
///
/// # Examples
///
/// ```no_run
/// #![feature(checked_duration_since)]
/// use std::time::{Duration, Instant};
/// use std::thread::sleep;
///
/// let now = Instant::now();
/// sleep(Duration::new(1, 0));
/// let new_now = Instant::now();
/// println!("{:?}", new_now.saturating_duration_since(now));
/// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
/// ```
#[unstable(feature = "checked_duration_since", issue = "58402")]
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0))
}

/// Returns the amount of time elapsed since this instant was created.
///
/// # Panics
Expand Down Expand Up @@ -626,6 +672,20 @@ mod tests {
(a - Duration::new(1, 0)).duration_since(a);
}

#[test]
fn checked_instant_duration_nopanic() {
let a = Instant::now();
let ret = (a - Duration::new(1, 0)).checked_duration_since(a);
assert_eq!(ret, None);
}

#[test]
fn saturating_instant_duration_nopanic() {
let a = Instant::now();
let ret = (a - Duration::new(1, 0)).saturating_duration_since(a);
assert_eq!(ret, Duration::new(0,0));
}

#[test]
fn system_time_math() {
let a = SystemTime::now();
Expand Down