pub trait EnsureDiv: EnsureDivAssign {
    // Provided method
    fn ensure_div(self, v: Self) -> Result<Self, ArithmeticError> { ... }
}
Expand description

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

Provided Methods§

source

fn ensure_div(self, v: Self) -> Result<Self, ArithmeticError>

Divides two numbers, checking for overflow.

If it fails, ArithmeticError is returned.

Similar to CheckedDiv::checked_div() but returning an ArithmeticError error.

Examples
use sp_arithmetic::traits::EnsureDiv;

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

assert_eq!(a.ensure_div(b), Ok(2));
use sp_arithmetic::{traits::EnsureDiv, ArithmeticError};

fn extrinsic_zero() -> Result<(), ArithmeticError> {
    1.ensure_div(0)?;
    Ok(())
}

fn overflow() -> Result<(), ArithmeticError> {
    i64::MIN.ensure_div(-1)?;
    Ok(())
}

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

Implementors§

source§

impl<T> EnsureDiv for Twhere T: EnsureDivAssign,