Merge rust-bitcoin/rust-bitcoin#2206: Add from_vb_const function
321d3923b8
Add from_vb_const function (yancy) Pull request description: This function is can be used to construct a Weight type from_vb in const context. Note I don't think it's possible to test the panic case since it's a compile time error work around currently to panic. ACKs for top commit: tcharding: ACK321d3923b8
apoelstra: ACK321d3923b8
Tree-SHA512: dc11409f0e3079400da261a8c9f580ef0527b77643ce1a5dda65c0975db19c2f2da46ac693e6a2bf49e0105b8b096e1ee51f09f5d1c78d634e4e274d7467ee05
This commit is contained in:
commit
aeac9bbd87
|
@ -54,6 +54,21 @@ impl Weight {
|
||||||
vb.checked_mul(Self::WITNESS_SCALE_FACTOR).map(Weight::from_wu)
|
vb.checked_mul(Self::WITNESS_SCALE_FACTOR).map(Weight::from_wu)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs `Weight` from virtual bytes in const context.
|
||||||
|
pub const fn from_vb_const(vb: u64) -> Weight {
|
||||||
|
match vb.checked_mul(Self::WITNESS_SCALE_FACTOR) {
|
||||||
|
Some(weight) => Weight(weight),
|
||||||
|
None => {
|
||||||
|
// TODO replace with panic!() when MSRV = 1.57+
|
||||||
|
#[allow(unconditional_panic)]
|
||||||
|
// disabling this lint until panic!() can be used.
|
||||||
|
#[allow(clippy::let_unit_value)]
|
||||||
|
let _int_overflow_scaling_weight = [(); 0][1];
|
||||||
|
Weight(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Constructs `Weight` from virtual bytes without an overflow check.
|
/// Constructs `Weight` from virtual bytes without an overflow check.
|
||||||
pub const fn from_vb_unchecked(vb: u64) -> Self { Weight::from_wu(vb * 4) }
|
pub const fn from_vb_unchecked(vb: u64) -> Self { Weight::from_wu(vb * 4) }
|
||||||
|
|
||||||
|
@ -150,6 +165,12 @@ mod tests {
|
||||||
assert_eq!(None, vb);
|
assert_eq!(None, vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_vb_const() {
|
||||||
|
const WU: Weight = Weight::from_vb_const(1);
|
||||||
|
assert_eq!(Weight(4), WU);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_vb_unchecked() {
|
fn from_vb_unchecked() {
|
||||||
let vb = Weight::from_vb_unchecked(1);
|
let vb = Weight::from_vb_unchecked(1);
|
||||||
|
|
Loading…
Reference in New Issue