Skip to content

Commit 7a6f363

Browse files
committed
Remove checked_sum impl for Weight
The `Weight` type does not use `NumOpResult<Weight>` with addition, `Add` has `Output = Weight` and uses wrapping addition. This is as expected and as desired. We do provide `Weight::checked_add` that returns an `Option<Weight>`, also as expected and as desired. Further context: `Amount` was deemed 'special' such that wrapping is considered to big a risk to have silent wrapping so we added the `NumOpResult` type. This argument does not apply to `Weight`. We would like to remove the `CheckedSum` trait because summing of `Amounts` can now be done with a combination of`Sum` and the `NumOpResult` type but `Weight` currently implements `CheckedSum`. For the reasons outline in regards to wrapping a `Weight` type, providing `CheckedSum` does not add much value - remove it. Done as preparation for removing the `CheckedSum` trait all together.
1 parent f7274a5 commit 7a6f363

File tree

1 file changed

+1
-20
lines changed

1 file changed

+1
-20
lines changed

units/src/weight.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use arbitrary::{Arbitrary, Unstructured};
1010
#[cfg(feature = "serde")]
1111
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1212

13-
use crate::{Amount, CheckedSum, FeeRate, NumOpResult};
13+
use crate::{Amount, FeeRate, NumOpResult};
1414

1515
/// The factor that non-witness serialization data is multiplied by during weight calculation.
1616
pub const WITNESS_SCALE_FACTOR: usize = 4;
@@ -251,13 +251,6 @@ impl ops::RemAssign<u64> for Weight {
251251
fn rem_assign(&mut self, rhs: u64) { *self = Weight::from_wu(self.to_wu() % rhs); }
252252
}
253253

254-
impl<T> CheckedSum<Weight> for T
255-
where
256-
T: Iterator<Item = Weight>,
257-
{
258-
fn checked_sum(mut self) -> Option<Weight> { self.try_fold(Weight::ZERO, Weight::checked_add) }
259-
}
260-
261254
impl core::iter::Sum for Weight {
262255
fn sum<I>(iter: I) -> Self
263256
where
@@ -541,16 +534,4 @@ mod tests {
541534
weight %= 3;
542535
assert_eq!(weight, Weight::from_wu(1));
543536
}
544-
545-
#[test]
546-
#[cfg(feature = "alloc")]
547-
fn checked_sum_weights() {
548-
assert_eq!([].into_iter().checked_sum(), Some(Weight::ZERO));
549-
550-
let sum = alloc::vec![0, 1, 2].iter().map(|&w| Weight::from_wu(w)).checked_sum().unwrap();
551-
assert_eq!(sum, Weight::from_wu(3));
552-
553-
let sum = alloc::vec![1, u64::MAX].iter().map(|&w| Weight::from_wu(w)).checked_sum();
554-
assert!(sum.is_none());
555-
}
556537
}

0 commit comments

Comments
 (0)