From 75594c72993960ba22d2bf38459f681659a9eb78 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 12 Dec 2024 11:08:59 +1100 Subject: [PATCH] Add Weight::to_kwu_ceil It is not immediately obvious why we have floor and ceil versions of `to_vbytes` but not for `to_kwu`. Add a conversion function to get weight by kilo weight units rounding up. --- units/src/weight.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/units/src/weight.rs b/units/src/weight.rs index 8c8827df7..c2599fb98 100644 --- a/units/src/weight.rs +++ b/units/src/weight.rs @@ -92,6 +92,9 @@ impl Weight { /// Converts to kilo weight units rounding down. pub const fn to_kwu_floor(self) -> u64 { self.0 / 1000 } + /// Converts to kilo weight units rounding up. + pub const fn to_kwu_ceil(self) -> u64 { (self.0 + 999) / 1000 } + /// Converts to vB rounding down. pub const fn to_vbytes_floor(self) -> u64 { self.0 / Self::WITNESS_SCALE_FACTOR } @@ -298,6 +301,13 @@ mod tests { #[test] fn to_kwu_floor() { assert_eq!(1, Weight(1_000).to_kwu_floor()); + assert_eq!(1, Weight(1_999).to_kwu_floor()); + } + + #[test] + fn to_kwu_ceil() { + assert_eq!(1, Weight(1_000).to_kwu_ceil()); + assert_eq!(2, Weight(1_001).to_kwu_ceil()); } #[test]