Trait sp_runtime::traits::EnsureMulAssign

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§

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));

Object Safety§

This trait is not object safe.

Implementors§

§

impl<T> EnsureMulAssign for T
where T: CheckedMul + PartialOrd + Zero,