Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libcore/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ fn checked_div() {
assert_eq!(Duration::new(2, 0).checked_div(0), None);
}

#[test]
fn correct_sum() {
let durations = [
Duration::new(1, 999_999_999),
Duration::new(2, 999_999_999),
Duration::new(0, 999_999_999),
Duration::new(0, 999_999_999),
Duration::new(0, 999_999_999),
Duration::new(5, 0),
];
let sum = durations.iter().sum::<Duration>();
assert_eq!(sum, Duration::new(1+2+5+4, 1_000_000_000 - 5));
}

#[test]
fn debug_formatting_extreme_values() {
assert_eq!(
Expand Down
34 changes: 32 additions & 2 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,47 @@ impl DivAssign<u32> for Duration {
}
}

macro_rules! sum_durations {
($iter:expr) => {{
let mut total_secs: u64 = 0;
let mut total_nanos: u64 = 0;

for entry in $iter {
total_secs = total_secs
.checked_add(entry.secs)
.expect("overflow in iter::sum over durations");
total_nanos = match total_nanos.checked_add(entry.nanos as u64) {
Some(n) => n,
None => {
total_secs = total_secs
.checked_add(total_nanos / NANOS_PER_SEC as u64)
.expect("overflow in iter::sum over durations");
(total_nanos % NANOS_PER_SEC as u64) + entry.nanos as u64
}
};
}
total_secs = total_secs
.checked_add(total_nanos / NANOS_PER_SEC as u64)
.expect("overflow in iter::sum over durations");
total_nanos = total_nanos % NANOS_PER_SEC as u64;
Duration {
secs: total_secs,
nanos: total_nanos as u32,
}
}};
}

#[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)
sum_durations!(iter)
}
}

#[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)
sum_durations!(iter)
}
}

Expand Down