referrerpolicy=no-referrer-when-downgrade

Trait polkadot_service::runtime_traits::EnsureFrom

source ·
pub trait EnsureFrom<T>: TryFrom<T> + PartialOrd + Zero
where 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, S> EnsureFrom<S> for T
where T: TryFrom<S> + PartialOrd + Zero, S: PartialOrd + Zero,