From 569cc8fff524f3b7142d86971b3d40de5525894d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 15 Dec 2021 11:30:41 +0100 Subject: [PATCH] Use the same Sum implementation for all number types --- packages/std/src/math/decimal.rs | 4 ++-- packages/std/src/math/decimal256.rs | 4 ++-- packages/std/src/math/uint128.rs | 16 ++++++---------- packages/std/src/math/uint256.rs | 16 ++++++---------- packages/std/src/math/uint512.rs | 16 ++++++---------- packages/std/src/math/uint64.rs | 16 ++++++---------- 6 files changed, 28 insertions(+), 44 deletions(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index b4e781397b..4617967331 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -361,10 +361,10 @@ impl ops::DivAssign for Decimal { impl std::iter::Sum for Decimal where - Decimal: ops::Add, + Self: ops::Add, { fn sum>(iter: I) -> Self { - iter.fold(Self::zero(), |a, b| a + b) + iter.fold(Self::zero(), ops::Add::add) } } diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 05dd4dc6a3..2da086762d 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -374,10 +374,10 @@ impl ops::DivAssign for Decimal256 { impl std::iter::Sum for Decimal256 where - Decimal256: ops::Add, + Self: ops::Add, { fn sum>(iter: I) -> Self { - iter.fold(Self::zero(), |a, b| a + b) + iter.fold(Self::zero(), ops::Add::add) } } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index ee78ff3f99..0548e5d232 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -2,7 +2,6 @@ use schemars::JsonSchema; use serde::{de, ser, Deserialize, Deserializer, Serialize}; use std::convert::{TryFrom, TryInto}; use std::fmt::{self}; -use std::iter::Sum; use std::ops; use std::str::FromStr; @@ -471,15 +470,12 @@ impl<'de> de::Visitor<'de> for Uint128Visitor { } } -impl Sum for Uint128 { - fn sum>(iter: I) -> Self { - iter.fold(Uint128::zero(), ops::Add::add) - } -} - -impl<'a> Sum<&'a Uint128> for Uint128 { - fn sum>(iter: I) -> Self { - iter.fold(Uint128::zero(), ops::Add::add) +impl std::iter::Sum for Uint128 +where + Self: ops::Add, +{ + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ops::Add::add) } } diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index cd422bc28f..463828caf8 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -2,7 +2,6 @@ use schemars::JsonSchema; use serde::{de, ser, Deserialize, Deserializer, Serialize}; use std::convert::{TryFrom, TryInto}; use std::fmt; -use std::iter::Sum; use std::ops::{self, Shl, Shr}; use std::str::FromStr; @@ -632,15 +631,12 @@ impl<'de> de::Visitor<'de> for Uint256Visitor { } } -impl Sum for Uint256 { - fn sum>(iter: I) -> Self { - iter.fold(Uint256::zero(), ops::Add::add) - } -} - -impl<'a> Sum<&'a Uint256> for Uint256 { - fn sum>(iter: I) -> Self { - iter.fold(Uint256::zero(), ops::Add::add) +impl std::iter::Sum for Uint256 +where + Self: ops::Add, +{ + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ops::Add::add) } } diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 1329989bb6..4eaef497bb 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -2,7 +2,6 @@ use schemars::JsonSchema; use serde::{de, ser, Deserialize, Deserializer, Serialize}; use std::convert::{TryFrom, TryInto}; use std::fmt; -use std::iter::Sum; use std::ops::{self, Shr}; use std::str::FromStr; @@ -659,15 +658,12 @@ impl<'de> de::Visitor<'de> for Uint512Visitor { } } -impl Sum for Uint512 { - fn sum>(iter: I) -> Self { - iter.fold(Uint512::zero(), ops::Add::add) - } -} - -impl<'a> Sum<&'a Uint512> for Uint512 { - fn sum>(iter: I) -> Self { - iter.fold(Uint512::zero(), ops::Add::add) +impl std::iter::Sum for Uint512 +where + Self: ops::Add, +{ + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ops::Add::add) } } diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 9b68c66867..3d0a149819 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -2,7 +2,6 @@ use schemars::JsonSchema; use serde::{de, ser, Deserialize, Deserializer, Serialize}; use std::convert::{TryFrom, TryInto}; use std::fmt::{self}; -use std::iter::Sum; use std::ops; use crate::errors::{DivideByZeroError, OverflowError, OverflowOperation, StdError}; @@ -355,15 +354,12 @@ impl<'de> de::Visitor<'de> for Uint64Visitor { } } -impl Sum for Uint64 { - fn sum>(iter: I) -> Self { - iter.fold(Uint64::zero(), ops::Add::add) - } -} - -impl<'a> Sum<&'a Uint64> for Uint64 { - fn sum>(iter: I) -> Self { - iter.fold(Uint64::zero(), ops::Add::add) +impl std::iter::Sum for Uint64 +where + Self: ops::Add, +{ + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ops::Add::add) } }