|
10 | 10 |
|
11 | 11 | //! Temporal quantification |
12 | 12 |
|
13 | | -use core::ops::{Add, Div, Mul, Neg, Sub}; |
| 13 | +use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; |
14 | 14 | use core::time::Duration as StdDuration; |
15 | 15 | use core::{fmt, i64}; |
16 | 16 | #[cfg(any(feature = "std", test))] |
@@ -380,6 +380,20 @@ impl Sub for Duration { |
380 | 380 | } |
381 | 381 | } |
382 | 382 |
|
| 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 | + |
383 | 397 | impl Mul<i32> for Duration { |
384 | 398 | type Output = Duration; |
385 | 399 |
|
@@ -534,6 +548,11 @@ mod tests { |
534 | 548 | -(Duration::days(3) + Duration::seconds(70)), |
535 | 549 | Duration::days(-4) + Duration::seconds(86_400 - 70) |
536 | 550 | ); |
| 551 | + |
| 552 | + let mut d = Duration::default(); |
| 553 | + d += Duration::minutes(1); |
| 554 | + d -= Duration::seconds(30); |
| 555 | + assert_eq!(d, Duration::seconds(30)); |
537 | 556 | } |
538 | 557 |
|
539 | 558 | #[test] |
|
0 commit comments