Skip to content

Commit

Permalink
Rollup merge of rust-lang#32448 - sfackler:time-augmented-assignment,…
Browse files Browse the repository at this point in the history
… r=alexcrichton

Add augmented assignment operator impls for time types

r? @alexcrichton
  • Loading branch information
Manishearth committed Mar 26, 2016
2 parents c5e3577 + be87650 commit f6001ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ops::{Add, Sub, Mul, Div};
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};

const NANOS_PER_SEC: u32 = 1_000_000_000;
const NANOS_PER_MILLI: u32 = 1_000_000;
Expand Down Expand Up @@ -105,6 +105,13 @@ impl Add for Duration {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl AddAssign for Duration {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}

#[stable(feature = "duration", since = "1.3.0")]
impl Sub for Duration {
type Output = Duration;
Expand All @@ -124,6 +131,13 @@ impl Sub for Duration {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl SubAssign for Duration {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}

#[stable(feature = "duration", since = "1.3.0")]
impl Mul<u32> for Duration {
type Output = Duration;
Expand All @@ -141,6 +155,13 @@ impl Mul<u32> for Duration {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
*self = *self * rhs;
}
}

#[stable(feature = "duration", since = "1.3.0")]
impl Div<u32> for Duration {
type Output = Duration;
Expand All @@ -155,6 +176,13 @@ impl Div<u32> for Duration {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl DivAssign<u32> for Duration {
fn div_assign(&mut self, rhs: u32) {
*self = *self / rhs;
}
}

#[cfg(test)]
mod tests {
use super::Duration;
Expand Down
30 changes: 29 additions & 1 deletion src/libstd/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use error::Error;
use fmt;
use ops::{Add, Sub};
use ops::{Add, Sub, AddAssign, SubAssign};
use sys::time;
use sys_common::FromInner;

Expand Down Expand Up @@ -172,6 +172,13 @@ impl Add<Duration> for Instant {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}

#[stable(feature = "time2", since = "1.8.0")]
impl Sub<Duration> for Instant {
type Output = Instant;
Expand All @@ -181,6 +188,13 @@ impl Sub<Duration> for Instant {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}

#[stable(feature = "time2", since = "1.8.0")]
impl Sub<Instant> for Instant {
type Output = Duration;
Expand Down Expand Up @@ -254,6 +268,13 @@ impl Add<Duration> for SystemTime {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl AddAssign<Duration> for SystemTime {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}

#[stable(feature = "time2", since = "1.8.0")]
impl Sub<Duration> for SystemTime {
type Output = SystemTime;
Expand All @@ -263,6 +284,13 @@ impl Sub<Duration> for SystemTime {
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl SubAssign<Duration> for SystemTime {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}

#[stable(feature = "time2", since = "1.8.0")]
impl fmt::Debug for SystemTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down

0 comments on commit f6001ce

Please sign in to comment.