Merge rust-bitcoin/rust-bitcoin#4181: Implement `Rem` for `Weight`

8007840676 Add a test for remainder (Jamil Lambert, PhD)
4787aa1f89 Implement Rem for Weight (Jamil Lambert, PhD)

Pull request description:

  Weight implements `Div` but not `Rem`.

  Add the `Rem` implementation.

  Add a test for the remainder operation on `Weight`

  Close #4171

ACKs for top commit:
  Kixunil:
    ACK 8007840676
  tcharding:
    ACK 8007840676
  apoelstra:
    ACK 80078406768aa5f8e4d21d42cc1d1fe9abeed1ea; successfully ran local tests

Tree-SHA512: cfcbc49a944146f7ba24a2f7b7f79ed2e2758ba2a23e9e69d9662afb5379dd74c646ff787c0e8218053e70410353cb74115e4c03bfdaafc8a1f9a4b0e51a3e15
This commit is contained in:
merge-script 2025-03-04 17:42:14 +00:00
commit 5f1700e79f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 33 additions and 0 deletions

View File

@ -198,6 +198,16 @@ crate::internal_macros::impl_op_for_references! {
fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() }
}
impl ops::Rem<u64> for Weight {
type Output = Weight;
fn rem(self, rhs: u64) -> Self::Output { Weight(self.0 % rhs) }
}
impl ops::Rem<Weight> for Weight {
type Output = u64;
fn rem(self, rhs: Weight) -> Self::Output { self.0 % rhs.0 }
}
}
crate::internal_macros::impl_add_assign!(Weight);
crate::internal_macros::impl_sub_assign!(Weight);
@ -210,6 +220,10 @@ impl ops::DivAssign<u64> for Weight {
fn div_assign(&mut self, rhs: u64) { self.0 /= rhs }
}
impl ops::RemAssign<u64> for Weight {
fn rem_assign(&mut self, rhs: u64) { self.0 %= rhs }
}
impl core::iter::Sum for Weight {
fn sum<I>(iter: I) -> Self
where
@ -439,4 +453,23 @@ mod tests {
w /= Weight(4).into();
assert_eq!(w, Weight(2));
}
#[test]
fn remainder() {
let weight10 = Weight(10);
let weight3 = Weight(3);
let remainder = weight10 % weight3;
assert_eq!(remainder, 1);
let remainder = weight10 % 3;
assert_eq!(remainder, Weight(1));
}
#[test]
fn remainder_assign() {
let mut weight = Weight(10);
weight %= 3;
assert_eq!(weight, Weight(1));
}
}