Skip to content

Commit 965ab29

Browse files
committed
Use checked_add and checked_sub in Add and Sub impls
1 parent e1cb286 commit 965ab29

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

src/duration.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -368,27 +368,15 @@ impl Add for Duration {
368368
type Output = Duration;
369369

370370
fn add(self, rhs: Duration) -> Duration {
371-
let mut secs = self.secs + rhs.secs;
372-
let mut nanos = self.nanos + rhs.nanos;
373-
if nanos >= NANOS_PER_SEC {
374-
nanos -= NANOS_PER_SEC;
375-
secs += 1;
376-
}
377-
Duration { secs, nanos }
371+
self.checked_add(&rhs).expect("`Duration + Duration` overflowed")
378372
}
379373
}
380374

381375
impl Sub for Duration {
382376
type Output = Duration;
383377

384378
fn sub(self, rhs: Duration) -> Duration {
385-
let mut secs = self.secs - rhs.secs;
386-
let mut nanos = self.nanos - rhs.nanos;
387-
if nanos < 0 {
388-
nanos += NANOS_PER_SEC;
389-
secs -= 1;
390-
}
391-
Duration { secs, nanos }
379+
self.checked_sub(&rhs).expect("`Duration - Duration` overflowed")
392380
}
393381
}
394382

0 commit comments

Comments
 (0)