Trait sp_runtime::traits::EnsureInto
source · pub trait EnsureInto<T>: TryInto<T> + PartialOrd<Self> + Zerowhere
T: PartialOrd<T> + Zero,{
// Provided method
fn ensure_into(self) -> Result<T, ArithmeticError> { ... }
}
Expand description
Similar to TryInto
but returning an ArithmeticError
error.
Provided Methods§
sourcefn ensure_into(self) -> Result<T, ArithmeticError>
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));