referrerpolicy=no-referrer-when-downgrade

Trait polkadot_sdk_frame::arithmetic::traits::EnsureMul

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§

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

Object Safety§

This trait is not object safe.

Implementors§

§

impl<T> EnsureMul for T
where T: EnsureMulAssign,