Skip to content

Commit 660a306

Browse files
committed
Merge rust-bitcoin#4695: Remove CheckedSum trait
bdf9e00 Remove CheckedSum trait (Tobin C. Harding) 7a6f363 Remove checked_sum impl for Weight (Tobin C. Harding) Pull request description: First remove the impl from `Weight` then remove the trait all together. Close: rust-bitcoin#4670 ACKs for top commit: apoelstra: ACK bdf9e00; successfully ran local tests Tree-SHA512: 2b0d8f506707ea69aee3d9d90d665e1ccae0193d2d4f86de1728e2c1ab19b2c73c834b32f509ef7ad66c2c1a38485e913aebfd6ec1570258978abd7a02a0db2e
2 parents a671834 + bdf9e00 commit 660a306

File tree

6 files changed

+34
-112
lines changed

6 files changed

+34
-112
lines changed

bitcoin/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ pub mod amount {
203203

204204
#[rustfmt::skip] // Keep public re-exports separate.
205205
#[doc(inline)]
206-
pub use units::CheckedSum;
207206
#[cfg(feature = "serde")]
208207
pub use units::amount::serde;
209208
pub use units::amount::{

units/src/amount/mod.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use core::str::FromStr;
2626
use arbitrary::{Arbitrary, Unstructured};
2727

2828
use self::error::{MissingDigitsKind, ParseAmountErrorInner, ParseErrorInner};
29-
use crate::CheckedSum;
3029

3130
#[rustfmt::skip] // Keep public re-exports separate.
3231
#[doc(inline)]
@@ -594,22 +593,6 @@ enum DisplayStyle {
594593
DynamicDenomination,
595594
}
596595

597-
impl<T> CheckedSum<Amount> for T
598-
where
599-
T: Iterator<Item = Amount>,
600-
{
601-
fn checked_sum(mut self) -> Option<Amount> { self.try_fold(Amount::ZERO, Amount::checked_add) }
602-
}
603-
604-
impl<T> CheckedSum<SignedAmount> for T
605-
where
606-
T: Iterator<Item = SignedAmount>,
607-
{
608-
fn checked_sum(mut self) -> Option<SignedAmount> {
609-
self.try_fold(SignedAmount::ZERO, SignedAmount::checked_add)
610-
}
611-
}
612-
613596
#[cfg(feature = "arbitrary")]
614597
impl<'a> Arbitrary<'a> for Denomination {
615598
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {

units/src/amount/tests.rs

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -841,68 +841,47 @@ fn to_string_with_denomination_from_str_roundtrip() {
841841

842842
#[test]
843843
fn sum_amounts() {
844-
assert_eq!([].iter().sum::<NumOpResult<Amount>>(), Amount::ZERO.into());
845-
assert_eq!([].iter().sum::<NumOpResult<SignedAmount>>(), SignedAmount::ZERO.into());
846-
847-
let results =
848-
[NumOpResult::Valid(sat(42)), NumOpResult::Valid(sat(1337)), NumOpResult::Valid(sat(21))];
849-
assert_eq!(results.iter().sum::<NumOpResult<Amount>>(), NumOpResult::Valid(sat(1400)));
850-
851-
let signed_results = [
852-
NumOpResult::Valid(ssat(42)),
853-
NumOpResult::Valid(ssat(1337)),
854-
NumOpResult::Valid(ssat(21)),
855-
];
856-
assert_eq!(
857-
signed_results.iter().sum::<NumOpResult<SignedAmount>>(),
858-
NumOpResult::Valid(ssat(1400))
859-
);
860-
861-
let amounts = [sat(42), sat(1337), sat(21)];
862-
assert_eq!(
863-
amounts.iter().map(|a| NumOpResult::Valid(*a)).sum::<NumOpResult<Amount>>(),
864-
sat(1400).into(),
865-
);
866-
assert_eq!(
867-
amounts.into_iter().map(NumOpResult::Valid).sum::<NumOpResult<Amount>>(),
868-
sat(1400).into(),
869-
);
870-
871-
let amounts = [ssat(-42), ssat(1337), ssat(21)];
872-
assert_eq!(
873-
amounts.iter().map(NumOpResult::from).sum::<NumOpResult<SignedAmount>>(),
874-
ssat(1316).into(),
875-
);
876-
assert_eq!(
877-
amounts.into_iter().map(NumOpResult::from).sum::<NumOpResult<SignedAmount>>(),
878-
ssat(1316).into()
879-
);
880-
}
881-
882-
#[test]
883-
fn checked_sum_amounts() {
884-
assert_eq!([].into_iter().checked_sum(), Some(Amount::ZERO));
885-
assert_eq!([].into_iter().checked_sum(), Some(SignedAmount::ZERO));
844+
let empty: [NumOpResult<Amount>; 0] = [];
845+
assert_eq!(empty.into_iter().sum::<NumOpResult<_>>(), NumOpResult::Valid(Amount::ZERO));
846+
let empty: [NumOpResult<SignedAmount>; 0] = [];
847+
assert_eq!(empty.into_iter().sum::<NumOpResult<_>>(), NumOpResult::Valid(SignedAmount::ZERO));
886848

887849
let amounts = [sat(42), sat(1337), sat(21)];
888-
let sum = amounts.into_iter().checked_sum();
889-
assert_eq!(sum, Some(sat(1400)));
850+
let sum = amounts
851+
.into_iter()
852+
.map(NumOpResult::from)
853+
.sum::<NumOpResult<Amount>>()
854+
.unwrap();
855+
assert_eq!(sum, sat(1400));
890856

891857
let amounts = [Amount::MAX_MONEY, sat(1337), sat(21)];
892-
let sum = amounts.into_iter().checked_sum();
893-
assert_eq!(sum, None);
858+
assert!(amounts
859+
.into_iter()
860+
.map(NumOpResult::from)
861+
.sum::<NumOpResult<Amount>>()
862+
.is_error());
894863

895864
let amounts = [SignedAmount::MIN, ssat(-1), ssat(21)];
896-
let sum = amounts.into_iter().checked_sum();
897-
assert_eq!(sum, None);
865+
assert!(amounts
866+
.into_iter()
867+
.map(NumOpResult::from)
868+
.sum::<NumOpResult<SignedAmount>>()
869+
.is_error());
898870

899871
let amounts = [SignedAmount::MAX, ssat(1), ssat(21)];
900-
let sum = amounts.into_iter().checked_sum();
901-
assert_eq!(sum, None);
872+
assert!(amounts
873+
.into_iter()
874+
.map(NumOpResult::from)
875+
.sum::<NumOpResult<SignedAmount>>()
876+
.is_error());
902877

903878
let amounts = [ssat(42), ssat(3301), ssat(21)];
904-
let sum = amounts.into_iter().checked_sum();
905-
assert_eq!(sum, Some(ssat(3364)));
879+
let sum = amounts
880+
.into_iter()
881+
.map(NumOpResult::from)
882+
.sum::<NumOpResult<SignedAmount>>()
883+
.unwrap();
884+
assert_eq!(sum, ssat(3364));
906885
}
907886

908887
#[test]

units/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,3 @@ pub(crate) use self::result::OptionExt;
6969
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
7070
#[doc(hidden)]
7171
pub type BlockInterval = BlockHeightInterval;
72-
73-
/// Calculates the sum over the iterator using checked arithmetic.
74-
pub trait CheckedSum<R>: sealed::Sealed<R> {
75-
/// Calculates the sum over the iterator using checked arithmetic. If an
76-
/// overflow happens it returns [`None`].
77-
fn checked_sum(self) -> Option<R>;
78-
}
79-
80-
mod sealed {
81-
use super::{Amount, SignedAmount, Weight};
82-
83-
/// Used to seal the `CheckedSum` trait
84-
pub trait Sealed<A> {}
85-
86-
impl<T> Sealed<Amount> for T where T: Iterator<Item = Amount> {}
87-
impl<T> Sealed<SignedAmount> for T where T: Iterator<Item = SignedAmount> {}
88-
impl<T> Sealed<Weight> for T where T: Iterator<Item = Weight> {}
89-
}

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
}

units/tests/api.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use arbitrary::{Arbitrary, Unstructured};
1515
use bitcoin_units::locktime::{absolute, relative}; // Typical usage is `absolute::LockTime`.
1616
use bitcoin_units::{
1717
amount, block, fee_rate, locktime, parse, time, weight, Amount, BlockHeight,
18-
BlockHeightInterval, BlockMtp, BlockMtpInterval, BlockTime, CheckedSum, FeeRate, MathOp,
19-
NumOpResult, SignedAmount, Weight,
18+
BlockHeightInterval, BlockMtp, BlockMtpInterval, BlockTime, FeeRate, MathOp, NumOpResult,
19+
SignedAmount, Weight,
2020
};
2121

2222
/// A struct that includes all public non-error enums.
@@ -283,8 +283,6 @@ fn regression_default() {
283283
fn dyn_compatible() {
284284
// If this builds then traits are dyn compatible.
285285
struct Traits {
286-
a: Box<dyn CheckedSum<Amount>>,
287-
b: Box<dyn CheckedSum<Weight>>,
288286
// These traits are explicitly not dyn compatible.
289287
// b: Box<dyn amount::serde::SerdeAmount>,
290288
// c: Box<dyn amount::serde::SerdeAmountForOpt>,

0 commit comments

Comments
 (0)