Merge rust-bitcoin/rust-bitcoin#1701: Fix weight subtract bug
b311e96603
Fix weight subtract bug (yancy) Pull request description: Fix subtraction bug ACKs for top commit: apoelstra: ACKb311e96603
tcharding: ACKb311e96603
sanket1729: utACKb311e96603
Tree-SHA512: 517b18432f88b501b17e8d8c9e11f6cfde76fc2c8cd7aba0afda79a4eea4a662154f4e09bb303386bcc93582921160b59642969514b6d9dce6cab62263b66b62
This commit is contained in:
commit
10d61f1c97
|
@ -86,7 +86,7 @@ impl Weight {
|
|||
///
|
||||
/// Computes `self - rhs` returning `None` if overflow occurred.
|
||||
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_add(rhs.0).map(Self)
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
|
||||
/// Checked multiplication.
|
||||
|
@ -115,6 +115,20 @@ impl fmt::Display for Weight {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn checked_sub_test(){
|
||||
let result = Weight(1).checked_sub(Weight(1)).expect("expected weight unit");
|
||||
assert_eq!(Weight::ZERO, result);
|
||||
|
||||
let result = Weight::MIN.checked_sub(Weight(1));
|
||||
assert_eq!(None, result);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Weight> for u64 {
|
||||
fn from(value: Weight) -> Self {
|
||||
value.to_wu()
|
||||
|
|
Loading…
Reference in New Issue