Mark funtions const

This commit is contained in:
yancy 2024-11-12 17:49:08 -06:00
parent 73e33e5808
commit 4c94695942
1 changed files with 28 additions and 4 deletions

View File

@ -101,22 +101,46 @@ impl Weight {
/// Checked addition. /// Checked addition.
/// ///
/// Computes `self + rhs` returning [`None`] if an overflow occurred. /// Computes `self + rhs` returning [`None`] if an overflow occurred.
pub fn checked_add(self, rhs: Self) -> Option<Self> { self.0.checked_add(rhs.0).map(Self) } pub const fn checked_add(self, rhs: Self) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_add(rhs.0) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}
/// Checked subtraction. /// Checked subtraction.
/// ///
/// Computes `self - rhs` returning [`None`] if an overflow occurred. /// Computes `self - rhs` returning [`None`] if an overflow occurred.
pub fn checked_sub(self, rhs: Self) -> Option<Self> { self.0.checked_sub(rhs.0).map(Self) } pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_sub(rhs.0) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}
/// Checked multiplication. /// Checked multiplication.
/// ///
/// Computes `self * rhs` returning [`None`] if an overflow occurred. /// Computes `self * rhs` returning [`None`] if an overflow occurred.
pub fn checked_mul(self, rhs: u64) -> Option<Self> { self.0.checked_mul(rhs).map(Self) } pub const fn checked_mul(self, rhs: u64) -> Option<Self> {
// No `map()` in const context.
match self.0.checked_mul(rhs) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None
}
}
/// Checked division. /// Checked division.
/// ///
/// Computes `self / rhs` returning [`None`] if `rhs == 0`. /// Computes `self / rhs` returning [`None`] if `rhs == 0`.
pub fn checked_div(self, rhs: u64) -> Option<Self> { self.0.checked_div(rhs).map(Self) } pub const fn checked_div(self, rhs: u64) -> Option<Self> {
// 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. /// Alternative will display the unit.