From 9502e2c82d732179ccdd98e1db121b9d8d91b590 Mon Sep 17 00:00:00 2001 From: Jake Vossen Date: Tue, 5 Jan 2021 13:23:46 -0700 Subject: [PATCH 1/3] added sum trait for Duration --- src/oldtime.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/oldtime.rs b/src/oldtime.rs index 388b71a47d..8e03657da3 100644 --- a/src/oldtime.rs +++ b/src/oldtime.rs @@ -373,6 +373,18 @@ impl Div for Duration { } } +impl<'a> std::iter::Sum<&'a Duration> for Duration { + fn sum>(iter: I) -> Duration { + iter.fold(Duration::zero(), |acc, x| acc + *x) + } +} + +impl std::iter::Sum for Duration { + fn sum>(iter: I) -> Duration { + iter.fold(Duration::zero(), |acc, x| acc + x) + } +} + impl fmt::Display for Duration { /// Format a duration using the [ISO 8601] format /// @@ -630,6 +642,27 @@ mod tests { assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333)); } + #[test] + fn test_duration_sum() { + let duration_list_1 = [Duration::zero(), Duration::seconds(1)]; + let sum_1: Duration = duration_list_1.iter().sum(); + assert_eq!(sum_1, Duration::seconds(1)); + + let duration_list_2 = + [Duration::zero(), Duration::seconds(1), Duration::seconds(6), Duration::seconds(10)]; + let sum_2: Duration = duration_list_2.iter().sum(); + assert_eq!(sum_2, Duration::seconds(17)); + + let duration_vec = vec![ + Duration::zero(), + Duration::seconds(1), + Duration::seconds(6), + Duration::seconds(10), + ]; + let sum_3: Duration = duration_vec.into_iter().sum(); + assert_eq!(sum_3, Duration::seconds(17)); + } + #[test] fn test_duration_fmt() { assert_eq!(Duration::zero().to_string(), "PT0S"); From 87e325418e1278d66ec5aec5476edd6b2b43bed4 Mon Sep 17 00:00:00 2001 From: Jake Vossen Date: Tue, 5 Jan 2021 13:27:05 -0700 Subject: [PATCH 2/3] added to changelog and authors --- AUTHORS.txt | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index 9501a9d059..5a07a48908 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -22,6 +22,7 @@ Eunchong Yu Frans Skarman Huon Wilson Igor Gnatenko +Jake Vossen Jim Turner Jisoo Park Joe Wilm diff --git a/CHANGELOG.md b/CHANGELOG.md index 16a9063b60..e922595d5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Versions with only mechanical changes will be omitted from the following list. * Add support for microseconds timestamps serde serialization/deserialization (#304) * Fix `DurationRound` is not TZ aware (#495) * Implement `DurationRound` for `NaiveDateTime` +* Implement `std::iter::Sum` for `Duration` ## 0.4.19 From d807ae5068dd649d3118430e1420f1160c1c1159 Mon Sep 17 00:00:00 2001 From: Jake Vossen Date: Fri, 22 Jan 2021 10:19:57 -0700 Subject: [PATCH 3/3] Sum only implemented when std is enabled --- src/oldtime.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/oldtime.rs b/src/oldtime.rs index 8e03657da3..0a28817795 100644 --- a/src/oldtime.rs +++ b/src/oldtime.rs @@ -373,12 +373,14 @@ impl Div for Duration { } } +#[cfg(any(feature = "std", test))] impl<'a> std::iter::Sum<&'a Duration> for Duration { fn sum>(iter: I) -> Duration { iter.fold(Duration::zero(), |acc, x| acc + *x) } } +#[cfg(any(feature = "std", test))] impl std::iter::Sum for Duration { fn sum>(iter: I) -> Duration { iter.fold(Duration::zero(), |acc, x| acc + x)