Skip to content

Commit 5f1700e

Browse files
committed
Merge rust-bitcoin#4181: Implement Rem for Weight
8007840 Add a test for remainder (Jamil Lambert, PhD) 4787aa1 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 rust-bitcoin#4171 ACKs for top commit: Kixunil: ACK 8007840 tcharding: ACK 8007840 apoelstra: ACK 8007840; successfully ran local tests Tree-SHA512: cfcbc49a944146f7ba24a2f7b7f79ed2e2758ba2a23e9e69d9662afb5379dd74c646ff787c0e8218053e70410353cb74115e4c03bfdaafc8a1f9a4b0e51a3e15
2 parents 95c012e + 8007840 commit 5f1700e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

units/src/weight.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ crate::internal_macros::impl_op_for_references! {
198198

199199
fn div(self, rhs: Weight) -> Self::Output { self.to_wu() / rhs.to_wu() }
200200
}
201+
impl ops::Rem<u64> for Weight {
202+
type Output = Weight;
203+
204+
fn rem(self, rhs: u64) -> Self::Output { Weight(self.0 % rhs) }
205+
}
206+
impl ops::Rem<Weight> for Weight {
207+
type Output = u64;
208+
209+
fn rem(self, rhs: Weight) -> Self::Output { self.0 % rhs.0 }
210+
}
201211
}
202212
crate::internal_macros::impl_add_assign!(Weight);
203213
crate::internal_macros::impl_sub_assign!(Weight);
@@ -210,6 +220,10 @@ impl ops::DivAssign<u64> for Weight {
210220
fn div_assign(&mut self, rhs: u64) { self.0 /= rhs }
211221
}
212222

223+
impl ops::RemAssign<u64> for Weight {
224+
fn rem_assign(&mut self, rhs: u64) { self.0 %= rhs }
225+
}
226+
213227
impl core::iter::Sum for Weight {
214228
fn sum<I>(iter: I) -> Self
215229
where
@@ -439,4 +453,23 @@ mod tests {
439453
w /= Weight(4).into();
440454
assert_eq!(w, Weight(2));
441455
}
456+
457+
#[test]
458+
fn remainder() {
459+
let weight10 = Weight(10);
460+
let weight3 = Weight(3);
461+
462+
let remainder = weight10 % weight3;
463+
assert_eq!(remainder, 1);
464+
465+
let remainder = weight10 % 3;
466+
assert_eq!(remainder, Weight(1));
467+
}
468+
469+
#[test]
470+
fn remainder_assign() {
471+
let mut weight = Weight(10);
472+
weight %= 3;
473+
assert_eq!(weight, Weight(1));
474+
}
442475
}

0 commit comments

Comments
 (0)