Skip to content

Commit e24e5af

Browse files
committed
Auto merge of rust-lang#117619 - elomatreb:add-duration-abs-diff, r=thomcc
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: rust-lang#117618
2 parents 7bd385d + 0ac438c commit e24e5af

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

library/core/src/time.rs

+21
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,27 @@ impl Duration {
461461
self.secs as u128 * NANOS_PER_SEC as u128 + self.nanos.0 as u128
462462
}
463463

464+
/// Computes the absolute difference between `self` and `other`.
465+
///
466+
/// # Examples
467+
///
468+
/// Basic usage:
469+
///
470+
/// ```
471+
/// #![feature(duration_abs_diff)]
472+
/// use std::time::Duration;
473+
///
474+
/// assert_eq!(Duration::new(100, 0).abs_diff(Duration::new(80, 0)), Duration::new(20, 0));
475+
/// assert_eq!(Duration::new(100, 400_000_000).abs_diff(Duration::new(110, 0)), Duration::new(9, 600_000_000));
476+
/// ```
477+
#[unstable(feature = "duration_abs_diff", issue = "117618")]
478+
#[must_use = "this returns the result of the operation, \
479+
without modifying the original"]
480+
#[inline]
481+
pub const fn abs_diff(self, other: Duration) -> Duration {
482+
if let Some(res) = self.checked_sub(other) { res } else { other.checked_sub(self).unwrap() }
483+
}
484+
464485
/// Checked `Duration` addition. Computes `self + other`, returning [`None`]
465486
/// if overflow occurred.
466487
///

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![feature(core_private_diy_float)]
2929
#![feature(dec2flt)]
3030
#![feature(div_duration)]
31+
#![feature(duration_abs_diff)]
3132
#![feature(duration_consts_float)]
3233
#![feature(duration_constants)]
3334
#![feature(exact_size_is_empty)]

library/core/tests/time.rs

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ fn nanos() {
7373
assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
7474
}
7575

76+
#[test]
77+
fn abs_diff() {
78+
assert_eq!(Duration::new(2, 0).abs_diff(Duration::new(1, 0)), Duration::new(1, 0));
79+
assert_eq!(Duration::new(1, 0).abs_diff(Duration::new(2, 0)), Duration::new(1, 0));
80+
assert_eq!(Duration::new(1, 0).abs_diff(Duration::new(1, 0)), Duration::new(0, 0));
81+
assert_eq!(Duration::new(1, 1).abs_diff(Duration::new(0, 2)), Duration::new(0, 999_999_999));
82+
assert_eq!(Duration::new(1, 1).abs_diff(Duration::new(2, 1)), Duration::new(1, 0));
83+
assert_eq!(Duration::MAX.abs_diff(Duration::MAX), Duration::ZERO);
84+
assert_eq!(Duration::ZERO.abs_diff(Duration::ZERO), Duration::ZERO);
85+
assert_eq!(Duration::MAX.abs_diff(Duration::ZERO), Duration::MAX);
86+
assert_eq!(Duration::ZERO.abs_diff(Duration::MAX), Duration::MAX);
87+
}
88+
7689
#[test]
7790
fn add() {
7891
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1), Duration::new(0, 1));

0 commit comments

Comments
 (0)