Skip to content

Commit

Permalink
Make NaiveTime::overflowing_sub_signed const
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jan 28, 2024
1 parent f0c55f7 commit 7900e1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ impl Duration {
}
Ok(StdDuration::new(self.secs as u64, self.nanos as u32))
}

/// This duplicates `Neg::neg` because trait methods can't be const yet.
pub(crate) const fn neg(self) -> Duration {
if self.nanos == 0 {
Duration { secs: -self.secs, nanos: 0 }
} else {
Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
}
}
}

impl Neg for Duration {
Expand Down
4 changes: 2 additions & 2 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ impl NaiveTime {
/// ```
#[inline]
#[must_use]
pub fn overflowing_sub_signed(&self, rhs: OldDuration) -> (NaiveTime, i64) {
let (time, rhs) = self.overflowing_add_signed(-rhs);
pub const fn overflowing_sub_signed(&self, rhs: OldDuration) -> (NaiveTime, i64) {
let (time, rhs) = self.overflowing_add_signed(rhs.neg());
(time, -rhs) // safe to negate, rhs is within +/- (2^63 / 1000)
}

Expand Down

0 comments on commit 7900e1d

Please sign in to comment.