pub trait EnsureFrom<T>: TryFrom<T> + PartialOrd<Self> + Zerowhere
    T: PartialOrd<T> + Zero,{
    // Provided method
    fn ensure_from(other: T) -> Result<Self, ArithmeticError> { ... }
}
Expand description

Similar to TryFrom but returning an ArithmeticError error.

Provided Methods§

source

fn ensure_from(other: T) -> Result<Self, ArithmeticError>

Performs the conversion returning an ArithmeticError if fails.

Similar to TryFrom::try_from() but returning an ArithmeticError error.

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

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

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

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

Implementors§

source§

impl<T, S> EnsureFrom<S> for Twhere T: TryFrom<S> + PartialOrd<T> + Zero, S: PartialOrd<S> + Zero,