pub trait EnsureFrom<T: PartialOrd + Zero>: TryFrom<T> + PartialOrd + 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));

Object Safety§

This trait is not object safe.

Implementors§

source§

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