Skip to content

Commit 6da23f4

Browse files
committed
Implement AddAssign and SubAssign
1 parent 38e50b3 commit 6da23f4

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(any(feature = "std", test))]
@@ -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

@@ -534,6 +548,11 @@ mod tests {
534548
-(Duration::days(3) + Duration::seconds(70)),
535549
Duration::days(-4) + Duration::seconds(86_400 - 70)
536550
);
551+
552+
let mut d = Duration::default();
553+
d += Duration::minutes(1);
554+
d -= Duration::seconds(30);
555+
assert_eq!(d, Duration::seconds(30));
537556
}
538557

539558
#[test]

src/naive/time/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,11 @@ impl NaiveTime {
554554
if frac >= 1_000_000_000 {
555555
let rfrac = 2_000_000_000 - frac;
556556
if rhs >= OldDuration::nanoseconds(i64::from(rfrac)) {
557-
rhs = rhs - OldDuration::nanoseconds(i64::from(rfrac));
557+
rhs -= OldDuration::nanoseconds(i64::from(rfrac));
558558
secs += 1;
559559
frac = 0;
560560
} else if rhs < OldDuration::nanoseconds(-i64::from(frac)) {
561-
rhs = rhs + OldDuration::nanoseconds(i64::from(frac));
561+
rhs += OldDuration::nanoseconds(i64::from(frac));
562562
frac = 0;
563563
} else {
564564
frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;

0 commit comments

Comments
 (0)