substrate icon indicating copy to clipboard operation
substrate copied to clipboard

Const impls of base arithmetics for `Weights` with `u64`

Open mustermeiszer opened this issue 3 years ago • 0 comments

Not sure, if this is essentially needed, but with respect to consts, which will be one of the common usages of Weights as runtime parameters, I am not a fan of saturating operations and the checked versions are currently not really usable as unwrap() is not available for consts.

The additional methods allow for usage of weights as const SOME_WEIGHT: Weight = OTHER_WEIGHT.div(2)

E.g.

  • const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2 does not work, which is fine.
  • const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.checked_div(2).unwrap() does not work, which is not so fine, as we don't want an Option<Weight>
  • const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_add(u64::MAX) does work, but assuming WEIGHT_PER_SECOND is bigger than u64::MAX a compilation error would be prefered .

mustermeiszer avatar Sep 21 '22 14:09 mustermeiszer