From 4c94695942de41e5eca4ac120db2e069c0bb037d Mon Sep 17 00:00:00 2001 From: yancy Date: Tue, 12 Nov 2024 17:49:08 -0600 Subject: [PATCH] Mark funtions const --- units/src/weight.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/units/src/weight.rs b/units/src/weight.rs index b43d763418..e8ae30b7c4 100644 --- a/units/src/weight.rs +++ b/units/src/weight.rs @@ -101,22 +101,46 @@ impl Weight { /// Checked addition. /// /// Computes `self + rhs` returning [`None`] if an overflow occurred. - pub fn checked_add(self, rhs: Self) -> Option { self.0.checked_add(rhs.0).map(Self) } + pub const fn checked_add(self, rhs: Self) -> Option { + // No `map()` in const context. + match self.0.checked_add(rhs.0) { + Some(wu) => Some(Weight::from_wu(wu)), + None => None + } + } /// Checked subtraction. /// /// Computes `self - rhs` returning [`None`] if an overflow occurred. - pub fn checked_sub(self, rhs: Self) -> Option { self.0.checked_sub(rhs.0).map(Self) } + pub const fn checked_sub(self, rhs: Self) -> Option { + // No `map()` in const context. + match self.0.checked_sub(rhs.0) { + Some(wu) => Some(Weight::from_wu(wu)), + None => None + } + } /// Checked multiplication. /// /// Computes `self * rhs` returning [`None`] if an overflow occurred. - pub fn checked_mul(self, rhs: u64) -> Option { self.0.checked_mul(rhs).map(Self) } + pub const fn checked_mul(self, rhs: u64) -> Option { + // No `map()` in const context. + match self.0.checked_mul(rhs) { + Some(wu) => Some(Weight::from_wu(wu)), + None => None + } + } /// Checked division. /// /// Computes `self / rhs` returning [`None`] if `rhs == 0`. - pub fn checked_div(self, rhs: u64) -> Option { self.0.checked_div(rhs).map(Self) } + pub const fn checked_div(self, rhs: u64) -> Option { + // No `map()` in const context. + match self.0.checked_div(rhs) { + Some(wu) => Some(Weight::from_wu(wu)), + None => None + } + } } /// Alternative will display the unit.