Skip to content

Commit

Permalink
Auto merge of #38712 - clarcharr:duration_sum, r=sfackler
Browse files Browse the repository at this point in the history
Sum for Duration

Implemented the `Sum` trait for `Duration`. Seems reasonable.
  • Loading branch information
bors committed Jan 19, 2017
2 parents c8af93f + 03b66ea commit 2263d1b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

const NANOS_PER_SEC: u32 = 1_000_000_000;
Expand Down Expand Up @@ -356,6 +357,20 @@ impl DivAssign<u32> for Duration {
}
}

#[stable(feature = "duration_sum", since = "1.16.0")]
impl Sum for Duration {
fn sum<I: Iterator<Item=Duration>>(iter: I) -> Duration {
iter.fold(Duration::new(0, 0), |a, b| a + b)
}
}

#[stable(feature = "duration_sum", since = "1.16.0")]
impl<'a> Sum<&'a Duration> for Duration {
fn sum<I: Iterator<Item=&'a Duration>>(iter: I) -> Duration {
iter.fold(Duration::new(0, 0), |a, b| a + *b)
}
}

#[cfg(test)]
mod tests {
use super::Duration;
Expand Down

0 comments on commit 2263d1b

Please sign in to comment.