Skip to content

Commit e5d3724

Browse files
committed
Implement AddAssign and SubAssign
1 parent bf4b6d8 commit e5d3724

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/duration.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Temporal quantification
1212
13-
use core::ops::{Add, Div, Mul, Neg, Sub};
13+
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
1414
use core::time::Duration as StdDuration;
1515
use core::{fmt, i64};
1616
#[cfg(feature = "std")]
@@ -380,6 +380,20 @@ impl Sub for Duration {
380380
}
381381
}
382382

383+
impl AddAssign for Duration {
384+
fn add_assign(&mut self, rhs: Duration) {
385+
let new = self.checked_add(&rhs).expect("`Duration + Duration` overflowed");
386+
*self = new;
387+
}
388+
}
389+
390+
impl SubAssign for Duration {
391+
fn sub_assign(&mut self, rhs: Duration) {
392+
let new = self.checked_sub(&rhs).expect("`Duration - Duration` overflowed");
393+
*self = new;
394+
}
395+
}
396+
383397
impl Mul<i32> for Duration {
384398
type Output = Duration;
385399

@@ -529,6 +543,11 @@ mod tests {
529543
-(Duration::days(3) + Duration::seconds(70)),
530544
Duration::days(-4) + Duration::seconds(86_400 - 70)
531545
);
546+
547+
let mut d = Duration::default();
548+
d += Duration::minutes(1);
549+
d -= Duration::seconds(30);
550+
assert_eq!(d, Duration::seconds(30));
532551
}
533552

534553
#[test]

src/naive/time/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,11 @@ impl NaiveTime {
567567
if frac >= 1_000_000_000 {
568568
let rfrac = 2_000_000_000 - frac;
569569
if rhs >= OldDuration::nanoseconds(i64::from(rfrac)) {
570-
rhs = rhs - OldDuration::nanoseconds(i64::from(rfrac));
570+
rhs -= OldDuration::nanoseconds(i64::from(rfrac));
571571
secs += 1;
572572
frac = 0;
573573
} else if rhs < OldDuration::nanoseconds(-i64::from(frac)) {
574-
rhs = rhs + OldDuration::nanoseconds(i64::from(frac));
574+
rhs += OldDuration::nanoseconds(i64::from(frac));
575575
frac = 0;
576576
} else {
577577
frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;

0 commit comments

Comments
 (0)