Trait sp_runtime::traits::EnsureDivAssign

source ·
pub trait EnsureDivAssign: CheckedDiv + PartialOrd + Zero {
    // Provided method
    fn ensure_div_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { ... }
}
Expand description

Performs self division that returns ArithmeticError instead of wrapping around on overflow.

Provided Methods§

source

fn ensure_div_assign(&mut self, v: Self) -> Result<(), ArithmeticError>

Divides two numbers overwriting the left hand one, checking for overflow.

If it fails, ArithmeticError is returned.

§Examples
use sp_arithmetic::traits::EnsureDivAssign;

let mut a: i32 = 20;
let b: i32 = 10;

a.ensure_div_assign(b).unwrap();
assert_eq!(a, 2);
use sp_arithmetic::{traits::EnsureDivAssign, ArithmeticError, FixedI64};

fn extrinsic_zero() -> Result<(), ArithmeticError> {
    let mut one = 1;
    one.ensure_div_assign(0)?;
    Ok(())
}

fn overflow() -> Result<(), ArithmeticError> {
    let mut min = FixedI64::from(i64::MIN);
    min.ensure_div_assign(FixedI64::from(-1))?;
    Ok(())
}

assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero));
assert_eq!(overflow(), Err(ArithmeticError::Overflow));

Object Safety§

This trait is not object safe.

Implementors§