Merge rust-bitcoin/rust-bitcoin#7038: feat(units): add saturating arithmetic methods to Weight
What changed, and why it matters
This commit adds safe math helpers to a Bitcoin library's Weight type. It lets developers add, subtract, and multiply weight values without worrying about overflow or underflow, because the result will clamp to the largest or smallest allowed value instead of wrapping around. There is no security flaw here; it is a routine feature addition with tests.
No security action needed. This is a benign feature addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces const saturating_add, saturating_sub, and saturating_mul methods on the Weight newtype in units/src/weight.rs. They delegate directly to u64’s saturating operations and are accompanied by unit tests covering normal cases, zero, and boundary saturation. No existing behavior is modified, no unsafe code is used, and no division method is added because unsigned division cannot overflow.
Changed components
units/src/weight.rsInspect captured patch +49 / −0
### units/src/weight.rs
@@ -180,6 +180,33 @@ impl Weight {
Some(Self::from_wu(wu))
}
+ /// Saturating addition.
+ ///
+ /// Computes `self + rhs`, saturating at [`Weight::MAX`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_add(self, rhs: Self) -> Self {
+ Self::from_wu(self.to_wu().saturating_add(rhs.to_wu()))
+ }
+
+ /// Saturating subtraction.
+ ///
+ /// Computes `self - rhs`, saturating at [`Weight::MIN`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_sub(self, rhs: Self) -> Self {
+ Self::from_wu(self.to_wu().saturating_sub(rhs.to_wu()))
+ }
+
+ /// Saturating multiplication.
+ ///
+ /// Computes `self * rhs`, saturating at [`Weight::MAX`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_mul(self, rhs: u64) -> Self {
+ Self::from_wu(self.to_wu().saturating_mul(rhs))
+ }
+
/// Checked fee rate multiplication.
///
/// Computes the absolute fee amount for a given [`FeeRate`] at this weight. When the resulting
@@ -490,6 +517,28 @@ mod tests {
#[test]
fn checked_div_overflows() { assert!(TWO.checked_div(0).is_none()) }
+ #[test]
+ fn saturating_add() {
+ assert_eq!(ONE.saturating_add(ONE), TWO);
+ assert_eq!(Weight::ZERO.saturating_add(Weight::ZERO), Weight::ZERO);
+ assert_eq!(Weight::MAX.saturating_add(ONE), Weight::MAX);
+ }
+
+ #[test]
+ fn saturating_sub() {
+ assert_eq!(TWO.saturating_sub(ONE), ONE);
+ assert_eq!(Weight::ZERO.saturating_sub(ONE), Weight::ZERO);
+ assert_eq!(ONE.saturating_sub(TWO), Weight::ZERO);
+ }
+
+ #[test]
+ fn saturating_mul() {
+ assert_eq!(TWO.saturating_mul(2), FOUR);
+ assert_eq!(TWO.saturating_mul(0), Weight::ZERO);
+ assert_eq!(Weight::MAX.saturating_mul(2), Weight::MAX);
+ assert_eq!(TWO.saturating_mul(u64::MAX), Weight::MAX);
+ }
+
#[test]
#[allow(clippy::op_ref)]
fn addition() {Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.