Skip to content
Merged
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
19 changes: 10 additions & 9 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Duration {
} else {
let secs = secs
.checked_add((nanos / NANOS_PER_SEC) as u64)
.expect("overflow in Duration::new");
.expect("adding carry-over seconds from nanoseconds should not exceed u64::MAX");
let nanos = nanos % NANOS_PER_SEC;
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
Duration { secs, nanos: unsafe { Nanoseconds::new_unchecked(nanos) } }
Expand Down Expand Up @@ -1342,7 +1342,7 @@ const impl Add for Duration {

#[inline]
fn add(self, rhs: Duration) -> Duration {
self.checked_add(rhs).expect("overflow when adding durations")
self.checked_add(rhs).expect("adding durations should not exceed u64::MAX")
}
}

Expand All @@ -1362,7 +1362,7 @@ const impl Sub for Duration {

#[inline]
fn sub(self, rhs: Duration) -> Duration {
self.checked_sub(rhs).expect("overflow when subtracting durations")
self.checked_sub(rhs).expect("subtracting durations should not be negative")
}
}

Expand All @@ -1382,7 +1382,7 @@ const impl Mul<u32> for Duration {

#[inline]
fn mul(self, rhs: u32) -> Duration {
self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
self.checked_mul(rhs).expect("multiplying duration by scalar should not exceed u64::MAX")
}
}

Expand Down Expand Up @@ -1414,7 +1414,7 @@ const impl Div<u32> for Duration {
#[inline]
#[track_caller]
fn div(self, rhs: u32) -> Duration {
self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
self.checked_div(rhs).expect("divisor should not be zero")
}
}

Expand All @@ -1434,21 +1434,22 @@ macro_rules! sum_durations {
let mut total_nanos: u64 = 0;

for entry in $iter {
total_secs =
total_secs.checked_add(entry.secs).expect("overflow in iter::sum over durations");
total_secs = total_secs
.checked_add(entry.secs)
.expect("sum over duration iterator should not overflow u64::MAX");
total_nanos = match total_nanos.checked_add(entry.nanos.as_inner() as u64) {
Some(n) => n,
None => {
total_secs = total_secs
.checked_add(total_nanos / NANOS_PER_SEC as u64)
.expect("overflow in iter::sum over durations");
.expect("sum over duration iterator should not overflow u64::MAX");
(total_nanos % NANOS_PER_SEC as u64) + entry.nanos.as_inner() as u64
}
};
}
total_secs = total_secs
.checked_add(total_nanos / NANOS_PER_SEC as u64)
.expect("overflow in iter::sum over durations");
.expect("sum over duration iterator should not overflow u64::MAX");
total_nanos %= NANOS_PER_SEC as u64;
Duration::new(total_secs, total_nanos as u32)
}};
Expand Down
Loading