Skip to content

Commit fdaa314

Browse files
committed
Implement AddAssign and SubAssign
1 parent e4c569c commit fdaa314

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
#[cfg(feature = "std")]
1515
use core::time::Duration as StdDuration;
1616
use core::{fmt, i64};
@@ -383,6 +383,20 @@ impl Sub for Duration {
383383
}
384384
}
385385

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

@@ -537,6 +551,11 @@ mod tests {
537551
-(Duration::days(3) + Duration::seconds(70)),
538552
Duration::days(-4) + Duration::seconds(86_400 - 70)
539553
);
554+
555+
let mut d = Duration::default();
556+
d += Duration::minutes(1);
557+
d -= Duration::seconds(30);
558+
assert_eq!(d, Duration::seconds(30));
540559
}
541560

542561
#[test]

src/naive/time/mod.rs

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

0 commit comments

Comments
 (0)