pub trait EnsureInto<T: PartialOrd + Zero>: TryInto<T> + PartialOrd + Zero {
    // Provided method
    fn ensure_into(self) -> Result<T, ArithmeticError> { ... }
}
Expand description

Similar to TryInto but returning an ArithmeticError error.

Provided Methods§

source

fn ensure_into(self) -> Result<T, ArithmeticError>

Performs the conversion returning an ArithmeticError if fails.

Similar to TryInto::try_into() but returning an ArithmeticError error

use sp_arithmetic::{traits::EnsureInto, ArithmeticError};

fn overflow() -> Result<(), ArithmeticError> {
    let byte: u8 = 256u16.ensure_into()?;
    Ok(())
}

fn underflow() -> Result<(), ArithmeticError> {
    let byte: i8 = (-129i16).ensure_into()?;
    Ok(())
}

assert_eq!(overflow(), Err(ArithmeticError::Overflow));
assert_eq!(underflow(), Err(ArithmeticError::Underflow));

Implementors§

source§

impl<T: TryInto<S> + PartialOrd + Zero, S: PartialOrd + Zero> EnsureInto<S> for T