Skip to content

Commit

Permalink
Auto merge of #117619 - elomatreb:add-duration-abs-diff, r=thomcc
Browse files Browse the repository at this point in the history
Add `Duration::abs_diff`

This adds a `Duration::abs_diff` method analogous to the existing one on the primitive integers.

ACP: rust-lang/libs-team#291
Tracking Issue: #117618
  • Loading branch information
bors committed Nov 21, 2023
2 parents 7bd385d + 0ac438c commit e24e5af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,27 @@ impl Duration {
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos.0 as u128
}

/// Computes the absolute difference between `self` and `other`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(duration_abs_diff)]
/// use std::time::Duration;
///
/// assert_eq!(Duration::new(100, 0).abs_diff(Duration::new(80, 0)), Duration::new(20, 0));
/// assert_eq!(Duration::new(100, 400_000_000).abs_diff(Duration::new(110, 0)), Duration::new(9, 600_000_000));
/// ```
#[unstable(feature = "duration_abs_diff", issue = "117618")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs_diff(self, other: Duration) -> Duration {
if let Some(res) = self.checked_sub(other) { res } else { other.checked_sub(self).unwrap() }
}

/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
/// if overflow occurred.
///
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#![feature(core_private_diy_float)]
#![feature(dec2flt)]
#![feature(div_duration)]
#![feature(duration_abs_diff)]
#![feature(duration_consts_float)]
#![feature(duration_constants)]
#![feature(exact_size_is_empty)]
Expand Down
13 changes: 13 additions & 0 deletions library/core/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ fn nanos() {
assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
}

#[test]
fn abs_diff() {
assert_eq!(Duration::new(2, 0).abs_diff(Duration::new(1, 0)), Duration::new(1, 0));
assert_eq!(Duration::new(1, 0).abs_diff(Duration::new(2, 0)), Duration::new(1, 0));
assert_eq!(Duration::new(1, 0).abs_diff(Duration::new(1, 0)), Duration::new(0, 0));
assert_eq!(Duration::new(1, 1).abs_diff(Duration::new(0, 2)), Duration::new(0, 999_999_999));
assert_eq!(Duration::new(1, 1).abs_diff(Duration::new(2, 1)), Duration::new(1, 0));
assert_eq!(Duration::MAX.abs_diff(Duration::MAX), Duration::ZERO);
assert_eq!(Duration::ZERO.abs_diff(Duration::ZERO), Duration::ZERO);
assert_eq!(Duration::MAX.abs_diff(Duration::ZERO), Duration::MAX);
assert_eq!(Duration::ZERO.abs_diff(Duration::MAX), Duration::MAX);
}

#[test]
fn add() {
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1), Duration::new(0, 1));
Expand Down

0 comments on commit e24e5af

Please sign in to comment.