Skip to content

Commit

Permalink
Rename abs_std to unsigned_abs and make public
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed May 6, 2022
1 parent f9398b9 commit c3df3f9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
9 changes: 9 additions & 0 deletions benchmarks/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ setup_benchmark! {
ben.iter(|| b.abs());
ben.iter(|| c.abs());
}

fn unsigned_abs(ben: &mut Bencher<'_, CyclesPerByte>) {
let a = 1.seconds();
let b = 0.seconds();
let c = (-1).seconds();
ben.iter(|| a.unsigned_abs());
ben.iter(|| b.unsigned_abs());
ben.iter(|| c.unsigned_abs());
}
// endregion abs

// region: constructors
Expand Down
16 changes: 11 additions & 5 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,17 @@ impl Duration {
Self::new_unchecked(self.seconds.saturating_abs(), self.nanoseconds.abs())
}

/// Convert the existing `Duration` to a `std::time::Duration` and its sign. This doesn't
/// actually require the standard library, but is currently only used when it's enabled.
#[allow(clippy::missing_const_for_fn)] // false positive
#[cfg(feature = "std")]
pub(crate) fn abs_std(self) -> StdDuration {
/// Convert the existing `Duration` to a `std::time::Duration` and its sign. This returns a
/// [`std::time::Duration`] and does not saturate the returned value (unlike [`Duration::abs`]).
///
/// ```rust
/// # use time::ext::{NumericalDuration, NumericalStdDuration};
/// assert_eq!(1.seconds().unsigned_abs(), 1.std_seconds());
/// assert_eq!(0.seconds().unsigned_abs(), 0.std_seconds());
/// assert_eq!((-1).seconds().unsigned_abs(), 1.std_seconds());
/// ```
#[allow(clippy::missing_const_for_fn)] // requires MSRV of 1.58 (2022-07-13)
pub fn unsigned_abs(self) -> StdDuration {
StdDuration::new(self.seconds.unsigned_abs(), self.nanoseconds.unsigned_abs())
}
// endregion abs
Expand Down
16 changes: 8 additions & 8 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ impl Instant {
if duration.is_zero() {
Some(self)
} else if duration.is_positive() {
self.0.checked_add(duration.abs_std()).map(Self)
self.0.checked_add(duration.unsigned_abs()).map(Self)
} else {
debug_assert!(duration.is_negative());
self.0.checked_sub(duration.abs_std()).map(Self)
self.0.checked_sub(duration.unsigned_abs()).map(Self)
}
}

Expand All @@ -94,10 +94,10 @@ impl Instant {
if duration.is_zero() {
Some(self)
} else if duration.is_positive() {
self.0.checked_sub(duration.abs_std()).map(Self)
self.0.checked_sub(duration.unsigned_abs()).map(Self)
} else {
debug_assert!(duration.is_negative());
self.0.checked_add(duration.abs_std()).map(Self)
self.0.checked_add(duration.unsigned_abs()).map(Self)
}
}
// endregion checked arithmetic
Expand Down Expand Up @@ -163,9 +163,9 @@ impl Add<Duration> for Instant {

fn add(self, duration: Duration) -> Self::Output {
if duration.is_positive() {
Self(self.0 + duration.abs_std())
Self(self.0 + duration.unsigned_abs())
} else if duration.is_negative() {
Self(self.0 - duration.abs_std())
Self(self.0 - duration.unsigned_abs())
} else {
self
}
Expand Down Expand Up @@ -196,9 +196,9 @@ impl Sub<Duration> for Instant {

fn sub(self, duration: Duration) -> Self::Output {
if duration.is_positive() {
Self(self.0 - duration.abs_std())
Self(self.0 - duration.unsigned_abs())
} else if duration.is_negative() {
Self(self.0 + duration.abs_std())
Self(self.0 + duration.unsigned_abs())
} else {
self
}
Expand Down
8 changes: 4 additions & 4 deletions src/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,10 @@ impl Add<Duration> for SystemTime {
if duration.is_zero() {
self
} else if duration.is_positive() {
self + duration.abs_std()
self + duration.unsigned_abs()
} else {
debug_assert!(duration.is_negative());
self - duration.abs_std()
self - duration.unsigned_abs()
}
}
}
Expand Down Expand Up @@ -1311,10 +1311,10 @@ impl From<OffsetDateTime> for SystemTime {
if duration.is_zero() {
Self::UNIX_EPOCH
} else if duration.is_positive() {
Self::UNIX_EPOCH + duration.abs_std()
Self::UNIX_EPOCH + duration.unsigned_abs()
} else {
debug_assert!(duration.is_negative());
Self::UNIX_EPOCH - duration.abs_std()
Self::UNIX_EPOCH - duration.unsigned_abs()
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ fn abs() {
assert_eq!((-1).seconds().abs(), 1.seconds());
}

#[test]
fn unsigned_abs() {
assert_eq!(1.seconds().unsigned_abs(), 1.std_seconds());
assert_eq!(0.seconds().unsigned_abs(), 0.std_seconds());
assert_eq!((-1).seconds().unsigned_abs(), 1.std_seconds());
}

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

0 comments on commit c3df3f9

Please sign in to comment.