Add from_vb_const function

This function can be used to construct a Weight type from_vb in const
context.
This commit is contained in:
yancy 2023-11-20 18:28:29 +01:00
parent 7246585588
commit 321d3923b8
1 changed files with 21 additions and 0 deletions

View File

@ -54,6 +54,21 @@ impl Weight {
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.
pub const fn from_vb_unchecked(vb: u64) -> Self { Weight::from_wu(vb * 4) }
@ -150,6 +165,12 @@ mod tests {
assert_eq!(None, vb);
}
#[test]
fn from_vb_const() {
const WU: Weight = Weight::from_vb_const(1);
assert_eq!(Weight(4), WU);
}
#[test]
fn from_vb_unchecked() {
let vb = Weight::from_vb_unchecked(1);