Trait sp_arithmetic::traits::EnsureFrom
source · 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§
sourcefn ensure_from(other: T) -> Result<Self, ArithmeticError>
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));