Trait sp_runtime::traits::EnsureMul
source · pub trait EnsureMul: EnsureMulAssign {
// Provided method
fn ensure_mul(self, v: Self) -> Result<Self, ArithmeticError> { ... }
}
Expand description
Performs multiplication that returns ArithmeticError
instead of wrapping around on
overflow.
Provided Methods§
sourcefn ensure_mul(self, v: Self) -> Result<Self, ArithmeticError>
fn ensure_mul(self, v: Self) -> Result<Self, ArithmeticError>
Multiplies two numbers, checking for overflow.
If it fails, ArithmeticError
is returned.
Similar to CheckedMul::checked_mul()
but returning an ArithmeticError
error.
Examples
use sp_arithmetic::traits::EnsureMul;
let a: i32 = 10;
let b: i32 = 20;
assert_eq!(a.ensure_mul(b), Ok(200));
use sp_arithmetic::{traits::EnsureMul, ArithmeticError};
fn overflow() -> Result<(), ArithmeticError> {
u32::MAX.ensure_mul(2)?;
Ok(())
}
fn underflow() -> Result<(), ArithmeticError> {
i32::MAX.ensure_mul(-2)?;
Ok(())
}
assert_eq!(overflow(), Err(ArithmeticError::Overflow));
assert_eq!(underflow(), Err(ArithmeticError::Underflow));