Skip to content

Commit f6001ce

Browse files
committed
Rollup merge of rust-lang#32448 - sfackler:time-augmented-assignment, r=alexcrichton
Add augmented assignment operator impls for time types r? @alexcrichton
2 parents c5e3577 + be87650 commit f6001ce

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/libstd/time/duration.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ops::{Add, Sub, Mul, Div};
11+
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
1212

1313
const NANOS_PER_SEC: u32 = 1_000_000_000;
1414
const NANOS_PER_MILLI: u32 = 1_000_000;
@@ -105,6 +105,13 @@ impl Add for Duration {
105105
}
106106
}
107107

108+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
109+
impl AddAssign for Duration {
110+
fn add_assign(&mut self, rhs: Duration) {
111+
*self = *self + rhs;
112+
}
113+
}
114+
108115
#[stable(feature = "duration", since = "1.3.0")]
109116
impl Sub for Duration {
110117
type Output = Duration;
@@ -124,6 +131,13 @@ impl Sub for Duration {
124131
}
125132
}
126133

134+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
135+
impl SubAssign for Duration {
136+
fn sub_assign(&mut self, rhs: Duration) {
137+
*self = *self - rhs;
138+
}
139+
}
140+
127141
#[stable(feature = "duration", since = "1.3.0")]
128142
impl Mul<u32> for Duration {
129143
type Output = Duration;
@@ -141,6 +155,13 @@ impl Mul<u32> for Duration {
141155
}
142156
}
143157

158+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
159+
impl MulAssign<u32> for Duration {
160+
fn mul_assign(&mut self, rhs: u32) {
161+
*self = *self * rhs;
162+
}
163+
}
164+
144165
#[stable(feature = "duration", since = "1.3.0")]
145166
impl Div<u32> for Duration {
146167
type Output = Duration;
@@ -155,6 +176,13 @@ impl Div<u32> for Duration {
155176
}
156177
}
157178

179+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
180+
impl DivAssign<u32> for Duration {
181+
fn div_assign(&mut self, rhs: u32) {
182+
*self = *self / rhs;
183+
}
184+
}
185+
158186
#[cfg(test)]
159187
mod tests {
160188
use super::Duration;

src/libstd/time/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
use error::Error;
2626
use fmt;
27-
use ops::{Add, Sub};
27+
use ops::{Add, Sub, AddAssign, SubAssign};
2828
use sys::time;
2929
use sys_common::FromInner;
3030

@@ -172,6 +172,13 @@ impl Add<Duration> for Instant {
172172
}
173173
}
174174

175+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
176+
impl AddAssign<Duration> for Instant {
177+
fn add_assign(&mut self, other: Duration) {
178+
*self = *self + other;
179+
}
180+
}
181+
175182
#[stable(feature = "time2", since = "1.8.0")]
176183
impl Sub<Duration> for Instant {
177184
type Output = Instant;
@@ -181,6 +188,13 @@ impl Sub<Duration> for Instant {
181188
}
182189
}
183190

191+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
192+
impl SubAssign<Duration> for Instant {
193+
fn sub_assign(&mut self, other: Duration) {
194+
*self = *self - other;
195+
}
196+
}
197+
184198
#[stable(feature = "time2", since = "1.8.0")]
185199
impl Sub<Instant> for Instant {
186200
type Output = Duration;
@@ -254,6 +268,13 @@ impl Add<Duration> for SystemTime {
254268
}
255269
}
256270

271+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
272+
impl AddAssign<Duration> for SystemTime {
273+
fn add_assign(&mut self, other: Duration) {
274+
*self = *self + other;
275+
}
276+
}
277+
257278
#[stable(feature = "time2", since = "1.8.0")]
258279
impl Sub<Duration> for SystemTime {
259280
type Output = SystemTime;
@@ -263,6 +284,13 @@ impl Sub<Duration> for SystemTime {
263284
}
264285
}
265286

287+
#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
288+
impl SubAssign<Duration> for SystemTime {
289+
fn sub_assign(&mut self, other: Duration) {
290+
*self = *self - other;
291+
}
292+
}
293+
266294
#[stable(feature = "time2", since = "1.8.0")]
267295
impl fmt::Debug for SystemTime {
268296
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)