Trait sp_arithmetic::traits::EnsureMulAssign
source · pub trait EnsureMulAssign: CheckedMul + PartialOrd + Zero {
// Provided method
fn ensure_mul_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { ... }
}
Expand description
Performs self multiplication that returns ArithmeticError
instead of wrapping around on
overflow.
Provided Methods§
sourcefn ensure_mul_assign(&mut self, v: Self) -> Result<(), ArithmeticError>
fn ensure_mul_assign(&mut self, v: Self) -> Result<(), ArithmeticError>
Multiplies two numbers overwriting the left hand one, checking for overflow.
If it fails, ArithmeticError
is returned.
Examples
use sp_arithmetic::traits::EnsureMulAssign;
let mut a: i32 = 10;
let b: i32 = 20;
a.ensure_mul_assign(b).unwrap();
assert_eq!(a, 200);
use sp_arithmetic::{traits::EnsureMulAssign, ArithmeticError};
fn overflow() -> Result<(), ArithmeticError> {
let mut max = u32::MAX;
max.ensure_mul_assign(2)?;
Ok(())
}
fn underflow() -> Result<(), ArithmeticError> {
let mut max = i32::MAX;
max.ensure_mul_assign(-2)?;
Ok(())
}
assert_eq!(overflow(), Err(ArithmeticError::Overflow));
assert_eq!(underflow(), Err(ArithmeticError::Underflow));