From 5684541a7087b9ebffd9a06746b9c2cf423642d7 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 19 Dec 2022 11:18:06 +0100 Subject: [PATCH 01/43] add ensure-ops family methods --- primitives/runtime/src/traits.rs | 502 +++++++++++++++++++++++++++++++ 1 file changed, 502 insertions(+) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 375475141b818..0fc8bee430a0e 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -1925,6 +1925,508 @@ pub trait BlockNumberProvider { fn set_block_number(_block: Self::BlockNumber) {} } +/// Arithmetic operations with safe error handling. +/// +/// This module provide a readable way to do safe arithmetics, turning this: +/// +/// ```ignore +/// self.my_value = self.my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; +/// ``` +/// +/// into this: +/// +/// ```ignore +/// self.my_value.ensure_sub_assign(other_value)?; +/// ``` +/// +/// choosing the correct [`ArithmeticError`] it should return in case of fail. +/// +/// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but +/// returning an [`ArithmeticError`] instead of `None`. +/// +/// [`ArithmeticError`]: sp_runtime::ArithmeticError +pub mod ensure { + use crate::ArithmeticError; + use sp_arithmetic::{ + traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}, + FixedPointNumber, FixedPointOperand, + }; + + /// Performs addition that returns `ArithmeticError` instead of wrapping around on overflow. + pub trait EnsureAdd: CheckedAdd + PartialOrd + Zero + Copy { + /// Adds two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureAdd; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// u32::MAX.ensure_add(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// i32::MIN.ensure_add(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_add(self, v: Self) -> Result { + self.checked_add(&v).ok_or_else(|| error::equivalent(v)) + } + } + + /// Performs subtraction that returns `ArithmeticError` instead of wrapping around on underflow. + pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { + /// Subtracts two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureSub; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// 0u32.ensure_sub(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// i32::MAX.ensure_sub(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_sub(self, v: Self) -> Result { + self.checked_sub(&v).ok_or_else(|| error::inverse(v)) + } + } + + /// Performs multiplication that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { + /// Multiplies two numbers, checking for overflow. If overflow happens, + /// `ArithmeticError` is returned. + /// + /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureMul; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// u32::MAX.ensure_mul(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// i32::MAX.ensure_mul(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul(self, v: Self) -> Result { + self.checked_mul(&v).ok_or_else(|| error::multiplication(self, v)) + } + } + + /// Performs division that returns `ArithmeticError` instead of wrapping around on overflow. + pub trait EnsureDiv: CheckedDiv + PartialOrd + Zero + Copy { + /// Divides two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureDiv; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// 1.ensure_div(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MIN).ensure_div(FixedI64::from(-1))?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div(self, v: Self) -> Result { + self.checked_div(&v).ok_or_else(|| error::division(self, v)) + } + } + + impl EnsureAdd for T {} + impl EnsureSub for T {} + impl EnsureMul for T {} + impl EnsureDiv for T {} + + /// Performs self addition that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureAddAssign: EnsureAdd { + /// Adds two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureAddAssign; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut max = u32::MAX; + /// max.ensure_add_assign(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut max = i32::MIN; + /// max.ensure_add_assign(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_add(v)?; + Ok(self) + } + } + + /// Performs self subtraction that returns `ArithmeticError` instead of wrapping around on + /// underflow. + pub trait EnsureSubAssign: EnsureSub { + /// Subtracts two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureSubAssign; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut zero: u32 = 0; + /// zero.ensure_sub_assign(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut zero = i32::MAX; + /// zero.ensure_sub_assign(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_sub(v)?; + Ok(self) + } + } + + /// Performs self multiplication that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureMulAssign: EnsureMul { + /// Multiplies two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureMulAssign; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut max = u32::MAX; + /// max.ensure_mul_assign(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut max = i32::MAX; + /// max.ensure_mul_assign(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_mul(v)?; + Ok(self) + } + } + + /// Performs self division that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureDivAssign: EnsureDiv { + /// Divides two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureDivAssign; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// let mut one = 1; + /// one.ensure_div_assign(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut min = FixedI64::from(i64::MIN); + /// min.ensure_div_assign(FixedI64::from(-1))?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_div(v)?; + Ok(self) + } + } + + impl EnsureAddAssign for T {} + impl EnsureSubAssign for T {} + impl EnsureMulAssign for T {} + impl EnsureDivAssign for T {} + + /// Extends `FixedPointNumber with` the Ensure family functions. + pub trait EnsureFixedPointNumber: FixedPointNumber { + /// Creates `self` from a rational number. Equal to `n / d`. + /// + /// Returns `ArithmeticError` if `d == 0` or `n / d` exceeds accuracy. + /// + /// Similar to [`FixedPointNumber::checked_from_rational()`] but returning an + /// `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// FixedI64::ensure_from_rational(1, 0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// FixedI64::ensure_from_rational(i64::MAX, -1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_from_rational( + n: N, + d: D, + ) -> Result { + ::checked_from_rational(n, d) + .ok_or_else(|| error::division(n, d)) + } + + /// Ensure multiplication for integer type `N`. Equal to `self * n`. + /// + /// Returns `ArithmeticError` if the result does not fit in `N`. + /// + /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an ArithmeticError + /// error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MAX).ensure_mul_int(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// FixedI64::from(i64::MAX).ensure_mul_int(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul_int(self, n: N) -> Result { + self.checked_mul_int(n).ok_or_else(|| error::multiplication(self, n)) + } + + /// Ensure division for integer type `N`. Equal to `self / d`. + /// + /// Returns `ArithmeticError` if the result does not fit in `N` or `d == 0`. + /// + /// Similar to [`FixedPointNumber::checked_div_int()`] but returning an `ArithmeticError` + /// error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// FixedI64::from(1).ensure_div_int(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MIN).ensure_div_int(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div_int(self, d: D) -> Result { + self.checked_div_int(d).ok_or_else(|| error::division(self, d)) + } + } + + impl EnsureFixedPointNumber for T {} + + /// Similar to [`TryFrom`] but returning an `ArithmeticError` error. + pub trait EnsureFrom: + TryFrom + PartialOrd + Zero + Copy + { + /// Performs the conversion returning an `ArithmeticError` if fails. + /// + /// Similar to [`TryFrom::try_from()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureFrom; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let byte: u8 = u8::ensure_from(256u16)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let byte: i8 = i8::ensure_from(-129i16)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_from(other: T) -> Result { + Self::try_from(other).map_err(|_| error::equivalent(other)) + } + } + + /// Similar to [`TryInto`] but returning an `ArithmeticError` error. + pub trait EnsureInto: + TryInto + PartialOrd + Zero + Copy + { + /// Performs the conversion returning an `ArithmeticError` if fails. + /// + /// Similar to [`TryInto::try_into()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_runtime::traits::ensure::EnsureInto; + /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let byte: u8 = 256u16.ensure_into()?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let byte: i8 = (-129i16).ensure_into()?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_into(self) -> Result { + self.try_into().map_err(|_| error::equivalent(self)) + } + } + + impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureFrom for T {} + impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureInto for T {} + + mod error { + use super::{ArithmeticError, Zero}; + + #[derive(PartialEq)] + enum Signum { + Negative, + Positive, + } + + impl From for Signum { + fn from(value: T) -> Self { + if value < Zero::zero() { + Signum::Negative + } else { + Signum::Positive + } + } + } + + impl sp_std::ops::Mul for Signum { + type Output = Self; + + fn mul(self, rhs: Self) -> Self { + if self != rhs { + Signum::Negative + } else { + Signum::Positive + } + } + } + + pub fn equivalent(r: R) -> ArithmeticError { + match Signum::from(r) { + Signum::Negative => ArithmeticError::Underflow, + Signum::Positive => ArithmeticError::Overflow, + } + } + + pub fn inverse(r: R) -> ArithmeticError { + match Signum::from(r) { + Signum::Negative => ArithmeticError::Overflow, + Signum::Positive => ArithmeticError::Underflow, + } + } + + pub fn multiplication( + l: L, + r: R, + ) -> ArithmeticError { + match Signum::from(l) * Signum::from(r) { + Signum::Negative => ArithmeticError::Underflow, + Signum::Positive => ArithmeticError::Overflow, + } + } + + pub fn division( + n: N, + d: D, + ) -> ArithmeticError { + if d.is_zero() { + ArithmeticError::DivisionByZero + } else { + multiplication(n, d) + } + } + } +} + #[cfg(test)] mod tests { use super::*; From 4d5291985d47148bf7d1deaa7cd30752518d1c86 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 19 Dec 2022 11:51:20 +0100 Subject: [PATCH 02/43] fix cargo doc --- primitives/runtime/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 0fc8bee430a0e..4e964af8c9633 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -1944,7 +1944,7 @@ pub trait BlockNumberProvider { /// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but /// returning an [`ArithmeticError`] instead of `None`. /// -/// [`ArithmeticError`]: sp_runtime::ArithmeticError +/// [`ArithmeticError`]: crate::ArithmeticError pub mod ensure { use crate::ArithmeticError; use sp_arithmetic::{ From e6ac8150332ed0cfa6bae8f752b9ed54459d824e Mon Sep 17 00:00:00 2001 From: lemunozm Date: Mon, 19 Dec 2022 12:58:11 +0100 Subject: [PATCH 03/43] add EnsureOp and EnsureOpAssign meta traits --- primitives/runtime/src/traits.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 4e964af8c9633..6f1507d287e21 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -2074,6 +2074,10 @@ pub mod ensure { impl EnsureMul for T {} impl EnsureDiv for T {} + /// Meta trait that supports all arithmetic operations + pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} + impl EnsureOp for T {} + /// Performs self addition that returns `ArithmeticError` instead of wrapping around on /// overflow. pub trait EnsureAddAssign: EnsureAdd { @@ -2203,6 +2207,16 @@ pub mod ensure { impl EnsureMulAssign for T {} impl EnsureDivAssign for T {} + /// Meta trait that supports all assigned arithmetic operations + pub trait EnsureOpAssign: + EnsureAddAssign + EnsureSubAssign + EnsureMulAssign + EnsureDivAssign + { + } + impl EnsureOpAssign + for T + { + } + /// Extends `FixedPointNumber with` the Ensure family functions. pub trait EnsureFixedPointNumber: FixedPointNumber { /// Creates `self` from a rational number. Equal to `n / d`. From 91976499292a8a88d43d8c119cc01846d738b4b8 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 08:59:29 +0100 Subject: [PATCH 04/43] move ensure module and ArithmeticError to sp-arithmetic --- primitives/arithmetic/src/lib.rs | 28 ++ primitives/arithmetic/src/traits.rs | 513 +++++++++++++++++++++++++++ primitives/runtime/src/lib.rs | 28 +- primitives/runtime/src/traits.rs | 516 ---------------------------- 4 files changed, 544 insertions(+), 541 deletions(-) diff --git a/primitives/arithmetic/src/lib.rs b/primitives/arithmetic/src/lib.rs index d7b326164b7d3..f1f8fc8b59830 100644 --- a/primitives/arithmetic/src/lib.rs +++ b/primitives/arithmetic/src/lib.rs @@ -50,6 +50,34 @@ pub use rational::{Rational128, RationalInfinite}; use sp_std::{cmp::Ordering, fmt::Debug, prelude::*}; use traits::{BaseArithmetic, One, SaturatedConversion, Unsigned, Zero}; +use codec::{Decode, Encode}; +use scale_info::TypeInfo; + +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; + +/// Arithmetic errors. +#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub enum ArithmeticError { + /// Underflow. + Underflow, + /// Overflow. + Overflow, + /// Division by zero. + DivisionByZero, +} + +impl From for &'static str { + fn from(e: ArithmeticError) -> &'static str { + match e { + ArithmeticError::Underflow => "An underflow would occur", + ArithmeticError::Overflow => "An overflow would occur", + ArithmeticError::DivisionByZero => "Division by zero", + } + } +} + /// Trait for comparing two numbers with an threshold. /// /// Returns: diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 466d5696c7136..8ccaab0d52e12 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -302,3 +302,516 @@ pub trait SaturatedConversion { } } impl SaturatedConversion for T {} + +/// Arithmetic operations with safe error handling. +/// +/// This module provide a readable way to do safe arithmetics, turning this: +/// +/// ```ignore +/// self.my_value = self.my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; +/// ``` +/// +/// into this: +/// +/// ```ignore +/// self.my_value.ensure_sub_assign(other_value)?; +/// ``` +/// +/// choosing the correct [`ArithmeticError`] it should return in case of fail. +/// +/// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but +/// returning an [`ArithmeticError`] instead of `None`. +/// +/// [`ArithmeticError`]: crate::ArithmeticError +pub mod ensure { + use super::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}; + use crate::{ArithmeticError, FixedPointNumber, FixedPointOperand}; + + /// Performs addition that returns `ArithmeticError` instead of wrapping around on overflow. + pub trait EnsureAdd: CheckedAdd + PartialOrd + Zero + Copy { + /// Adds two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureAdd, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// u32::MAX.ensure_add(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// i32::MIN.ensure_add(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_add(self, v: Self) -> Result { + self.checked_add(&v).ok_or_else(|| error::equivalent(v)) + } + } + + /// Performs subtraction that returns `ArithmeticError` instead of wrapping around on underflow. + pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { + /// Subtracts two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// 0u32.ensure_sub(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// i32::MAX.ensure_sub(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_sub(self, v: Self) -> Result { + self.checked_sub(&v).ok_or_else(|| error::inverse(v)) + } + } + + /// Performs multiplication that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { + /// Multiplies two numbers, checking for overflow. If overflow happens, + /// `ArithmeticError` is returned. + /// + /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureMul, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// u32::MAX.ensure_mul(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// i32::MAX.ensure_mul(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul(self, v: Self) -> Result { + self.checked_mul(&v).ok_or_else(|| error::multiplication(self, v)) + } + } + + /// Performs division that returns `ArithmeticError` instead of wrapping around on overflow. + pub trait EnsureDiv: CheckedDiv + PartialOrd + Zero + Copy { + /// Divides two numbers, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureDiv, ArithmeticError, FixedI64}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// 1.ensure_div(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MIN).ensure_div(FixedI64::from(-1))?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div(self, v: Self) -> Result { + self.checked_div(&v).ok_or_else(|| error::division(self, v)) + } + } + + impl EnsureAdd for T {} + impl EnsureSub for T {} + impl EnsureMul for T {} + impl EnsureDiv for T {} + + /// Meta trait that supports all arithmetic operations + pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} + impl EnsureOp for T {} + + /// Performs self addition that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureAddAssign: EnsureAdd { + /// Adds two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureAddAssign, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut max = u32::MAX; + /// max.ensure_add_assign(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut max = i32::MIN; + /// max.ensure_add_assign(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_add(v)?; + Ok(self) + } + } + + /// Performs self subtraction that returns `ArithmeticError` instead of wrapping around on + /// underflow. + pub trait EnsureSubAssign: EnsureSub { + /// Subtracts two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureSubAssign, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut zero: u32 = 0; + /// zero.ensure_sub_assign(1)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut zero = i32::MAX; + /// zero.ensure_sub_assign(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_sub(v)?; + Ok(self) + } + } + + /// Performs self multiplication that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureMulAssign: EnsureMul { + /// Multiplies two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureMulAssign, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut max = u32::MAX; + /// max.ensure_mul_assign(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let mut max = i32::MAX; + /// max.ensure_mul_assign(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_mul(v)?; + Ok(self) + } + } + + /// Performs self division that returns `ArithmeticError` instead of wrapping around on + /// overflow. + pub trait EnsureDivAssign: EnsureDiv { + /// Divides two numbers overwriting the left hand one, checking for overflow. + /// If overflow happens, `ArithmeticError` is returned. + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureDivAssign, ArithmeticError, FixedI64}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// let mut one = 1; + /// one.ensure_div_assign(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let mut min = FixedI64::from(i64::MIN); + /// min.ensure_div_assign(FixedI64::from(-1))?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + *self = self.ensure_div(v)?; + Ok(self) + } + } + + impl EnsureAddAssign for T {} + impl EnsureSubAssign for T {} + impl EnsureMulAssign for T {} + impl EnsureDivAssign for T {} + + /// Meta trait that supports all assigned arithmetic operations + pub trait EnsureOpAssign: + EnsureAddAssign + EnsureSubAssign + EnsureMulAssign + EnsureDivAssign + { + } + impl EnsureOpAssign + for T + { + } + + /// Extends `FixedPointNumber with` the Ensure family functions. + pub trait EnsureFixedPointNumber: FixedPointNumber { + /// Creates `self` from a rational number. Equal to `n / d`. + /// + /// Returns `ArithmeticError` if `d == 0` or `n / d` exceeds accuracy. + /// + /// Similar to [`FixedPointNumber::checked_from_rational()`] but returning an + /// `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// FixedI64::ensure_from_rational(1, 0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// FixedI64::ensure_from_rational(i64::MAX, -1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_from_rational( + n: N, + d: D, + ) -> Result { + ::checked_from_rational(n, d) + .ok_or_else(|| error::division(n, d)) + } + + /// Ensure multiplication for integer type `N`. Equal to `self * n`. + /// + /// Returns `ArithmeticError` if the result does not fit in `N`. + /// + /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an ArithmeticError + /// error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MAX).ensure_mul_int(2)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// FixedI64::from(i64::MAX).ensure_mul_int(-2)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_mul_int(self, n: N) -> Result { + self.checked_mul_int(n).ok_or_else(|| error::multiplication(self, n)) + } + + /// Ensure division for integer type `N`. Equal to `self / d`. + /// + /// Returns `ArithmeticError` if the result does not fit in `N` or `d == 0`. + /// + /// Similar to [`FixedPointNumber::checked_div_int()`] but returning an `ArithmeticError` + /// error + /// + /// ``` + /// use sp_arithmetic::traits::ensure::{EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_zero() -> DispatchResult { + /// FixedI64::from(1).ensure_div_int(0)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// FixedI64::from(i64::MIN).ensure_div_int(-1)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// ``` + fn ensure_div_int(self, d: D) -> Result { + self.checked_div_int(d).ok_or_else(|| error::division(self, d)) + } + } + + impl EnsureFixedPointNumber for T {} + + /// Similar to [`TryFrom`] but returning an `ArithmeticError` error. + pub trait EnsureFrom: + TryFrom + PartialOrd + Zero + Copy + { + /// Performs the conversion returning an `ArithmeticError` if fails. + /// + /// Similar to [`TryFrom::try_from()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureFrom, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let byte: u8 = u8::ensure_from(256u16)?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let byte: i8 = i8::ensure_from(-129i16)?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_from(other: T) -> Result { + Self::try_from(other).map_err(|_| error::equivalent(other)) + } + } + + /// Similar to [`TryInto`] but returning an `ArithmeticError` error. + pub trait EnsureInto: + TryInto + PartialOrd + Zero + Copy + { + /// Performs the conversion returning an `ArithmeticError` if fails. + /// + /// Similar to [`TryInto::try_into()`] but returning an `ArithmeticError` error + /// + /// ``` + /// use sp_arithmetic::{traits::ensure::EnsureInto, ArithmeticError}; + /// use sp_runtime::DispatchResult; + /// + /// fn extrinsic_overflow() -> DispatchResult { + /// let byte: u8 = 256u16.ensure_into()?; + /// Ok(()) + /// } + /// + /// fn extrinsic_underflow() -> DispatchResult { + /// let byte: i8 = (-129i16).ensure_into()?; + /// Ok(()) + /// } + /// + /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// ``` + fn ensure_into(self) -> Result { + self.try_into().map_err(|_| error::equivalent(self)) + } + } + + impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureFrom for T {} + impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureInto for T {} + + mod error { + use super::{ArithmeticError, Zero}; + + #[derive(PartialEq)] + enum Signum { + Negative, + Positive, + } + + impl From for Signum { + fn from(value: T) -> Self { + if value < Zero::zero() { + Signum::Negative + } else { + Signum::Positive + } + } + } + + impl sp_std::ops::Mul for Signum { + type Output = Self; + + fn mul(self, rhs: Self) -> Self { + if self != rhs { + Signum::Negative + } else { + Signum::Positive + } + } + } + + pub fn equivalent(r: R) -> ArithmeticError { + match Signum::from(r) { + Signum::Negative => ArithmeticError::Underflow, + Signum::Positive => ArithmeticError::Overflow, + } + } + + pub fn inverse(r: R) -> ArithmeticError { + match Signum::from(r) { + Signum::Negative => ArithmeticError::Overflow, + Signum::Positive => ArithmeticError::Underflow, + } + } + + pub fn multiplication( + l: L, + r: R, + ) -> ArithmeticError { + match Signum::from(l) * Signum::from(r) { + Signum::Negative => ArithmeticError::Underflow, + Signum::Positive => ArithmeticError::Overflow, + } + } + + pub fn division( + n: N, + d: D, + ) -> ArithmeticError { + if d.is_zero() { + ArithmeticError::DivisionByZero + } else { + multiplication(n, d) + } + } + } +} diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index e94efda86aa03..96fe7d2487f48 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -93,9 +93,9 @@ pub use sp_arithmetic::biguint; pub use sp_arithmetic::helpers_128bit; /// Re-export top-level arithmetic stuff. pub use sp_arithmetic::{ - traits::SaturatedConversion, FixedI128, FixedI64, FixedPointNumber, FixedPointOperand, - FixedU128, InnerOf, PerThing, PerU16, Perbill, Percent, Permill, Perquintill, Rational128, - Rounding, UpperOf, + traits::SaturatedConversion, ArithmeticError, FixedI128, FixedI64, FixedPointNumber, + FixedPointOperand, FixedU128, InnerOf, PerThing, PerU16, Perbill, Percent, Permill, + Perquintill, Rational128, Rounding, UpperOf, }; pub use either::Either; @@ -641,28 +641,6 @@ impl From for DispatchError { } } -/// Arithmetic errors. -#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum ArithmeticError { - /// Underflow. - Underflow, - /// Overflow. - Overflow, - /// Division by zero. - DivisionByZero, -} - -impl From for &'static str { - fn from(e: ArithmeticError) -> &'static str { - match e { - ArithmeticError::Underflow => "An underflow would occur", - ArithmeticError::Overflow => "An overflow would occur", - ArithmeticError::DivisionByZero => "Division by zero", - } - } -} - impl From for DispatchError { fn from(e: ArithmeticError) -> DispatchError { Self::Arithmetic(e) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 6f1507d287e21..375475141b818 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -1925,522 +1925,6 @@ pub trait BlockNumberProvider { fn set_block_number(_block: Self::BlockNumber) {} } -/// Arithmetic operations with safe error handling. -/// -/// This module provide a readable way to do safe arithmetics, turning this: -/// -/// ```ignore -/// self.my_value = self.my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; -/// ``` -/// -/// into this: -/// -/// ```ignore -/// self.my_value.ensure_sub_assign(other_value)?; -/// ``` -/// -/// choosing the correct [`ArithmeticError`] it should return in case of fail. -/// -/// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but -/// returning an [`ArithmeticError`] instead of `None`. -/// -/// [`ArithmeticError`]: crate::ArithmeticError -pub mod ensure { - use crate::ArithmeticError; - use sp_arithmetic::{ - traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}, - FixedPointNumber, FixedPointOperand, - }; - - /// Performs addition that returns `ArithmeticError` instead of wrapping around on overflow. - pub trait EnsureAdd: CheckedAdd + PartialOrd + Zero + Copy { - /// Adds two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureAdd; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// u32::MAX.ensure_add(1)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// i32::MIN.ensure_add(-1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_add(self, v: Self) -> Result { - self.checked_add(&v).ok_or_else(|| error::equivalent(v)) - } - } - - /// Performs subtraction that returns `ArithmeticError` instead of wrapping around on underflow. - pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { - /// Subtracts two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureSub; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// 0u32.ensure_sub(1)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// i32::MAX.ensure_sub(-1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// ``` - fn ensure_sub(self, v: Self) -> Result { - self.checked_sub(&v).ok_or_else(|| error::inverse(v)) - } - } - - /// Performs multiplication that returns `ArithmeticError` instead of wrapping around on - /// overflow. - pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { - /// Multiplies two numbers, checking for overflow. If overflow happens, - /// `ArithmeticError` is returned. - /// - /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureMul; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// u32::MAX.ensure_mul(2)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// i32::MAX.ensure_mul(-2)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_mul(self, v: Self) -> Result { - self.checked_mul(&v).ok_or_else(|| error::multiplication(self, v)) - } - } - - /// Performs division that returns `ArithmeticError` instead of wrapping around on overflow. - pub trait EnsureDiv: CheckedDiv + PartialOrd + Zero + Copy { - /// Divides two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureDiv; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; - /// - /// fn extrinsic_zero() -> DispatchResult { - /// 1.ensure_div(0)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// FixedI64::from(i64::MIN).ensure_div(FixedI64::from(-1))?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// ``` - fn ensure_div(self, v: Self) -> Result { - self.checked_div(&v).ok_or_else(|| error::division(self, v)) - } - } - - impl EnsureAdd for T {} - impl EnsureSub for T {} - impl EnsureMul for T {} - impl EnsureDiv for T {} - - /// Meta trait that supports all arithmetic operations - pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} - impl EnsureOp for T {} - - /// Performs self addition that returns `ArithmeticError` instead of wrapping around on - /// overflow. - pub trait EnsureAddAssign: EnsureAdd { - /// Adds two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureAddAssign; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let mut max = u32::MAX; - /// max.ensure_add_assign(1)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// let mut max = i32::MIN; - /// max.ensure_add_assign(-1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { - *self = self.ensure_add(v)?; - Ok(self) - } - } - - /// Performs self subtraction that returns `ArithmeticError` instead of wrapping around on - /// underflow. - pub trait EnsureSubAssign: EnsureSub { - /// Subtracts two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureSubAssign; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// let mut zero: u32 = 0; - /// zero.ensure_sub_assign(1)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let mut zero = i32::MAX; - /// zero.ensure_sub_assign(-1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// ``` - fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { - *self = self.ensure_sub(v)?; - Ok(self) - } - } - - /// Performs self multiplication that returns `ArithmeticError` instead of wrapping around on - /// overflow. - pub trait EnsureMulAssign: EnsureMul { - /// Multiplies two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureMulAssign; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let mut max = u32::MAX; - /// max.ensure_mul_assign(2)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// let mut max = i32::MAX; - /// max.ensure_mul_assign(-2)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { - *self = self.ensure_mul(v)?; - Ok(self) - } - } - - /// Performs self division that returns `ArithmeticError` instead of wrapping around on - /// overflow. - pub trait EnsureDivAssign: EnsureDiv { - /// Divides two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureDivAssign; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; - /// - /// fn extrinsic_zero() -> DispatchResult { - /// let mut one = 1; - /// one.ensure_div_assign(0)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let mut min = FixedI64::from(i64::MIN); - /// min.ensure_div_assign(FixedI64::from(-1))?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// ``` - fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { - *self = self.ensure_div(v)?; - Ok(self) - } - } - - impl EnsureAddAssign for T {} - impl EnsureSubAssign for T {} - impl EnsureMulAssign for T {} - impl EnsureDivAssign for T {} - - /// Meta trait that supports all assigned arithmetic operations - pub trait EnsureOpAssign: - EnsureAddAssign + EnsureSubAssign + EnsureMulAssign + EnsureDivAssign - { - } - impl EnsureOpAssign - for T - { - } - - /// Extends `FixedPointNumber with` the Ensure family functions. - pub trait EnsureFixedPointNumber: FixedPointNumber { - /// Creates `self` from a rational number. Equal to `n / d`. - /// - /// Returns `ArithmeticError` if `d == 0` or `n / d` exceeds accuracy. - /// - /// Similar to [`FixedPointNumber::checked_from_rational()`] but returning an - /// `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; - /// - /// fn extrinsic_zero() -> DispatchResult { - /// FixedI64::ensure_from_rational(1, 0)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// FixedI64::ensure_from_rational(i64::MAX, -1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_from_rational( - n: N, - d: D, - ) -> Result { - ::checked_from_rational(n, d) - .ok_or_else(|| error::division(n, d)) - } - - /// Ensure multiplication for integer type `N`. Equal to `self * n`. - /// - /// Returns `ArithmeticError` if the result does not fit in `N`. - /// - /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an ArithmeticError - /// error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// FixedI64::from(i64::MAX).ensure_mul_int(2)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// FixedI64::from(i64::MAX).ensure_mul_int(-2)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_mul_int(self, n: N) -> Result { - self.checked_mul_int(n).ok_or_else(|| error::multiplication(self, n)) - } - - /// Ensure division for integer type `N`. Equal to `self / d`. - /// - /// Returns `ArithmeticError` if the result does not fit in `N` or `d == 0`. - /// - /// Similar to [`FixedPointNumber::checked_div_int()`] but returning an `ArithmeticError` - /// error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureFixedPointNumber; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError, FixedI64}; - /// - /// fn extrinsic_zero() -> DispatchResult { - /// FixedI64::from(1).ensure_div_int(0)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// FixedI64::from(i64::MIN).ensure_div_int(-1)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// ``` - fn ensure_div_int(self, d: D) -> Result { - self.checked_div_int(d).ok_or_else(|| error::division(self, d)) - } - } - - impl EnsureFixedPointNumber for T {} - - /// Similar to [`TryFrom`] but returning an `ArithmeticError` error. - pub trait EnsureFrom: - TryFrom + PartialOrd + Zero + Copy - { - /// Performs the conversion returning an `ArithmeticError` if fails. - /// - /// Similar to [`TryFrom::try_from()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureFrom; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let byte: u8 = u8::ensure_from(256u16)?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// let byte: i8 = i8::ensure_from(-129i16)?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_from(other: T) -> Result { - Self::try_from(other).map_err(|_| error::equivalent(other)) - } - } - - /// Similar to [`TryInto`] but returning an `ArithmeticError` error. - pub trait EnsureInto: - TryInto + PartialOrd + Zero + Copy - { - /// Performs the conversion returning an `ArithmeticError` if fails. - /// - /// Similar to [`TryInto::try_into()`] but returning an `ArithmeticError` error - /// - /// ``` - /// use sp_runtime::traits::ensure::EnsureInto; - /// use sp_runtime::{DispatchResult, ArithmeticError, DispatchError}; - /// - /// fn extrinsic_overflow() -> DispatchResult { - /// let byte: u8 = 256u16.ensure_into()?; - /// Ok(()) - /// } - /// - /// fn extrinsic_underflow() -> DispatchResult { - /// let byte: i8 = (-129i16).ensure_into()?; - /// Ok(()) - /// } - /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// ``` - fn ensure_into(self) -> Result { - self.try_into().map_err(|_| error::equivalent(self)) - } - } - - impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureFrom for T {} - impl + PartialOrd + Zero + Copy, S: PartialOrd + Zero + Copy> EnsureInto for T {} - - mod error { - use super::{ArithmeticError, Zero}; - - #[derive(PartialEq)] - enum Signum { - Negative, - Positive, - } - - impl From for Signum { - fn from(value: T) -> Self { - if value < Zero::zero() { - Signum::Negative - } else { - Signum::Positive - } - } - } - - impl sp_std::ops::Mul for Signum { - type Output = Self; - - fn mul(self, rhs: Self) -> Self { - if self != rhs { - Signum::Negative - } else { - Signum::Positive - } - } - } - - pub fn equivalent(r: R) -> ArithmeticError { - match Signum::from(r) { - Signum::Negative => ArithmeticError::Underflow, - Signum::Positive => ArithmeticError::Overflow, - } - } - - pub fn inverse(r: R) -> ArithmeticError { - match Signum::from(r) { - Signum::Negative => ArithmeticError::Overflow, - Signum::Positive => ArithmeticError::Underflow, - } - } - - pub fn multiplication( - l: L, - r: R, - ) -> ArithmeticError { - match Signum::from(l) * Signum::from(r) { - Signum::Negative => ArithmeticError::Underflow, - Signum::Positive => ArithmeticError::Overflow, - } - } - - pub fn division( - n: N, - d: D, - ) -> ArithmeticError { - if d.is_zero() { - ArithmeticError::DivisionByZero - } else { - multiplication(n, d) - } - } - } -} - #[cfg(test)] mod tests { use super::*; From 61f2fc7d123fcffa07f18eed7b63fe054b539ed9 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 09:49:32 +0100 Subject: [PATCH 05/43] fix doc examples --- primitives/arithmetic/src/traits.rs | 131 ++++++++++++++-------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 8ccaab0d52e12..ac426fee1e93a 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -307,14 +307,26 @@ impl SaturatedConversion for T {} /// /// This module provide a readable way to do safe arithmetics, turning this: /// -/// ```ignore -/// self.my_value = self.my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; +/// ``` +/// # use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; +/// # fn foo() -> Result<(), ArithmeticError> { +/// # let mut my_value = 1; +/// # let other_value = 1; +/// my_value = my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; +/// # Ok(()) +/// # } /// ``` /// /// into this: /// -/// ```ignore -/// self.my_value.ensure_sub_assign(other_value)?; +/// ``` +/// # use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; +/// # fn foo() -> Result<(), ArithmeticError> { +/// # let mut my_value = 1; +/// # let other_value = 1; +/// my_value.ensure_sub_assign(other_value)?; +/// # Ok(()) +/// # } /// ``` /// /// choosing the correct [`ArithmeticError`] it should return in case of fail. @@ -336,20 +348,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureAdd, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// u32::MAX.ensure_add(1)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// i32::MIN.ensure_add(-1)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_add(self, v: Self) -> Result { self.checked_add(&v).ok_or_else(|| error::equivalent(v)) @@ -365,20 +376,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// 0u32.ensure_sub(1)?; /// Ok(()) /// } /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// i32::MAX.ensure_sub(-1)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); /// ``` fn ensure_sub(self, v: Self) -> Result { self.checked_sub(&v).ok_or_else(|| error::inverse(v)) @@ -395,20 +405,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureMul, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// u32::MAX.ensure_mul(2)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// i32::MAX.ensure_mul(-2)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_mul(self, v: Self) -> Result { self.checked_mul(&v).ok_or_else(|| error::multiplication(self, v)) @@ -424,20 +433,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureDiv, ArithmeticError, FixedI64}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_zero() -> DispatchResult { + /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// 1.ensure_div(0)?; /// Ok(()) /// } /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// FixedI64::from(i64::MIN).ensure_div(FixedI64::from(-1))?; /// Ok(()) /// } /// /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); /// ``` fn ensure_div(self, v: Self) -> Result { self.checked_div(&v).ok_or_else(|| error::division(self, v)) @@ -461,22 +469,21 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureAddAssign, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let mut max = u32::MAX; /// max.ensure_add_assign(1)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// let mut max = i32::MIN; /// max.ensure_add_assign(-1)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_add(v)?; @@ -492,22 +499,21 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureSubAssign, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// let mut zero: u32 = 0; /// zero.ensure_sub_assign(1)?; /// Ok(()) /// } /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let mut zero = i32::MAX; /// zero.ensure_sub_assign(-1)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); /// ``` fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_sub(v)?; @@ -523,22 +529,21 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureMulAssign, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let mut max = u32::MAX; /// max.ensure_mul_assign(2)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// let mut max = i32::MAX; /// max.ensure_mul_assign(-2)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_mul(v)?; @@ -554,22 +559,21 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureDivAssign, ArithmeticError, FixedI64}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_zero() -> DispatchResult { + /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// let mut one = 1; /// one.ensure_div_assign(0)?; /// Ok(()) /// } /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let mut min = FixedI64::from(i64::MIN); /// min.ensure_div_assign(FixedI64::from(-1))?; /// Ok(()) /// } /// /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); /// ``` fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_div(v)?; @@ -603,20 +607,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_zero() -> DispatchResult { + /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// FixedI64::ensure_from_rational(1, 0)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// FixedI64::ensure_from_rational(i64::MAX, -1)?; /// Ok(()) /// } /// /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_from_rational( n: N, @@ -635,20 +638,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// FixedI64::from(i64::MAX).ensure_mul_int(2)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// FixedI64::from(i64::MAX).ensure_mul_int(-2)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_mul_int(self, n: N) -> Result { self.checked_mul_int(n).ok_or_else(|| error::multiplication(self, n)) @@ -662,21 +664,20 @@ pub mod ensure { /// error /// /// ``` - /// use sp_arithmetic::traits::ensure::{EnsureFixedPointNumber, ArithmeticError, FixedI64}; - /// use sp_runtime::DispatchResult; + /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; /// - /// fn extrinsic_zero() -> DispatchResult { + /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// FixedI64::from(1).ensure_div_int(0)?; /// Ok(()) /// } /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// FixedI64::from(i64::MIN).ensure_div_int(-1)?; /// Ok(()) /// } /// /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); /// ``` fn ensure_div_int(self, d: D) -> Result { self.checked_div_int(d).ok_or_else(|| error::division(self, d)) @@ -695,20 +696,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureFrom, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let byte: u8 = u8::ensure_from(256u16)?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// let byte: i8 = i8::ensure_from(-129i16)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_from(other: T) -> Result { Self::try_from(other).map_err(|_| error::equivalent(other)) @@ -725,20 +725,19 @@ pub mod ensure { /// /// ``` /// use sp_arithmetic::{traits::ensure::EnsureInto, ArithmeticError}; - /// use sp_runtime::DispatchResult; /// - /// fn extrinsic_overflow() -> DispatchResult { + /// fn overflow() -> Result<(), ArithmeticError> { /// let byte: u8 = 256u16.ensure_into()?; /// Ok(()) /// } /// - /// fn extrinsic_underflow() -> DispatchResult { + /// fn underflow() -> Result<(), ArithmeticError> { /// let byte: i8 = (-129i16).ensure_into()?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(extrinsic_underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); /// ``` fn ensure_into(self) -> Result { self.try_into().map_err(|_| error::equivalent(self)) From a3e9649a8a57fa2d02a04c3a3ca8908d66edab5c Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 10:07:06 +0100 Subject: [PATCH 06/43] reexport ensure module content --- primitives/arithmetic/src/traits.rs | 34 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index ac426fee1e93a..886c44d143619 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -18,6 +18,10 @@ //! Primitive traits for the runtime arithmetic. use codec::HasCompact; +pub use ensure::{ + EnsureAdd, EnsureAddAssign, EnsureDiv, EnsureDivAssign, EnsureFixedPointNumber, EnsureFrom, + EnsureInto, EnsureMul, EnsureMulAssign, EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, +}; pub use integer_sqrt::IntegerSquareRoot; pub use num_traits::{ checked_pow, Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, @@ -308,7 +312,7 @@ impl SaturatedConversion for T {} /// This module provide a readable way to do safe arithmetics, turning this: /// /// ``` -/// # use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; +/// # use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; /// # fn foo() -> Result<(), ArithmeticError> { /// # let mut my_value = 1; /// # let other_value = 1; @@ -320,7 +324,7 @@ impl SaturatedConversion for T {} /// into this: /// /// ``` -/// # use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; +/// # use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; /// # fn foo() -> Result<(), ArithmeticError> { /// # let mut my_value = 1; /// # let other_value = 1; @@ -347,7 +351,7 @@ pub mod ensure { /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureAdd, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureAdd, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// u32::MAX.ensure_add(1)?; @@ -375,7 +379,7 @@ pub mod ensure { /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureSub, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; /// /// fn underflow() -> Result<(), ArithmeticError> { /// 0u32.ensure_sub(1)?; @@ -404,7 +408,7 @@ pub mod ensure { /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureMul, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureMul, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// u32::MAX.ensure_mul(2)?; @@ -432,7 +436,7 @@ pub mod ensure { /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureDiv, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureDiv, ArithmeticError, FixedI64}; /// /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// 1.ensure_div(0)?; @@ -468,7 +472,7 @@ pub mod ensure { /// If overflow happens, `ArithmeticError` is returned. /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureAddAssign, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureAddAssign, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// let mut max = u32::MAX; @@ -498,7 +502,7 @@ pub mod ensure { /// If overflow happens, `ArithmeticError` is returned. /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureSubAssign, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureSubAssign, ArithmeticError}; /// /// fn underflow() -> Result<(), ArithmeticError> { /// let mut zero: u32 = 0; @@ -528,7 +532,7 @@ pub mod ensure { /// If overflow happens, `ArithmeticError` is returned. /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureMulAssign, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureMulAssign, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// let mut max = u32::MAX; @@ -558,7 +562,7 @@ pub mod ensure { /// If overflow happens, `ArithmeticError` is returned. /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureDivAssign, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureDivAssign, ArithmeticError, FixedI64}; /// /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// let mut one = 1; @@ -606,7 +610,7 @@ pub mod ensure { /// `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; /// /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// FixedI64::ensure_from_rational(1, 0)?; @@ -637,7 +641,7 @@ pub mod ensure { /// error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// FixedI64::from(i64::MAX).ensure_mul_int(2)?; @@ -664,7 +668,7 @@ pub mod ensure { /// error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureFixedPointNumber, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; /// /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// FixedI64::from(1).ensure_div_int(0)?; @@ -695,7 +699,7 @@ pub mod ensure { /// Similar to [`TryFrom::try_from()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureFrom, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureFrom, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// let byte: u8 = u8::ensure_from(256u16)?; @@ -724,7 +728,7 @@ pub mod ensure { /// Similar to [`TryInto::try_into()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::ensure::EnsureInto, ArithmeticError}; + /// use sp_arithmetic::{traits::EnsureInto, ArithmeticError}; /// /// fn overflow() -> Result<(), ArithmeticError> { /// let byte: u8 = 256u16.ensure_into()?; From 36feea613df9c57092310bd663429726010394da Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 10:15:03 +0100 Subject: [PATCH 07/43] ensure mod private --- primitives/arithmetic/src/traits.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 886c44d143619..a9400301924db 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -19,8 +19,9 @@ use codec::HasCompact; pub use ensure::{ - EnsureAdd, EnsureAddAssign, EnsureDiv, EnsureDivAssign, EnsureFixedPointNumber, EnsureFrom, - EnsureInto, EnsureMul, EnsureMulAssign, EnsureOp, EnsureOpAssign, EnsureSub, EnsureSubAssign, + Ensure, EnsureAdd, EnsureAddAssign, EnsureDiv, EnsureDivAssign, EnsureFixedPointNumber, + EnsureFrom, EnsureInto, EnsureMul, EnsureMulAssign, EnsureOp, EnsureOpAssign, EnsureSub, + EnsureSubAssign, }; pub use integer_sqrt::IntegerSquareRoot; pub use num_traits::{ @@ -339,7 +340,7 @@ impl SaturatedConversion for T {} /// returning an [`ArithmeticError`] instead of `None`. /// /// [`ArithmeticError`]: crate::ArithmeticError -pub mod ensure { +mod ensure { use super::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}; use crate::{ArithmeticError, FixedPointNumber, FixedPointOperand}; @@ -461,7 +462,7 @@ pub mod ensure { impl EnsureMul for T {} impl EnsureDiv for T {} - /// Meta trait that supports all arithmetic operations + /// Meta trait that supports all inmutable arithmetic operations pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} impl EnsureOp for T {} @@ -600,6 +601,10 @@ pub mod ensure { { } + /// Meta trait that supports all arithmetic operations + pub trait Ensure: EnsureOp + EnsureOpAssign {} + impl Ensure for T {} + /// Extends `FixedPointNumber with` the Ensure family functions. pub trait EnsureFixedPointNumber: FixedPointNumber { /// Creates `self` from a rational number. Equal to `n / d`. From 43b8814a89d09f8499858a278650d08f0241eef4 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 10:21:48 +0100 Subject: [PATCH 08/43] reexport to sp-runtime --- primitives/runtime/src/traits.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 375475141b818..8978cdb11c0c6 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -33,8 +33,10 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use sp_application_crypto::AppKey; pub use sp_arithmetic::traits::{ AtLeast32Bit, AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedShl, - CheckedShr, CheckedSub, IntegerSquareRoot, One, SaturatedConversion, Saturating, - UniqueSaturatedFrom, UniqueSaturatedInto, Zero, + CheckedShr, CheckedSub, Ensure, EnsureAdd, EnsureAddAssign, EnsureDiv, EnsureDivAssign, + EnsureFixedPointNumber, EnsureFrom, EnsureInto, EnsureMul, EnsureMulAssign, EnsureOp, + EnsureOpAssign, EnsureSub, EnsureSubAssign, IntegerSquareRoot, One, SaturatedConversion, + Saturating, UniqueSaturatedFrom, UniqueSaturatedInto, Zero, }; use sp_core::{self, storage::StateVersion, Hasher, RuntimeDebug, TypeId}; #[doc(hidden)] From e4c15142dc0cc922ae9798ebb52928cf03a6abb9 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 11:07:16 +0100 Subject: [PATCH 09/43] fix doc example --- primitives/arithmetic/src/traits.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index a9400301924db..519146439dcdd 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -315,8 +315,8 @@ impl SaturatedConversion for T {} /// ``` /// # use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; /// # fn foo() -> Result<(), ArithmeticError> { -/// # let mut my_value = 1; -/// # let other_value = 1; +/// # let mut my_value: i32 = 1; +/// # let other_value: i32 = 1; /// my_value = my_value.checked_sub(other_value).ok_or(ArithmeticError::Overflow)?; /// # Ok(()) /// # } @@ -325,10 +325,10 @@ impl SaturatedConversion for T {} /// into this: /// /// ``` -/// # use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; +/// # use sp_arithmetic::{traits::EnsureSubAssign, ArithmeticError}; /// # fn foo() -> Result<(), ArithmeticError> { -/// # let mut my_value = 1; -/// # let other_value = 1; +/// # let mut my_value: i32 = 1; +/// # let other_value: i32 = 1; /// my_value.ensure_sub_assign(other_value)?; /// # Ok(()) /// # } From 0a2ee21c29bda10d13587ae6aee63d71e66d4f44 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 20 Dec 2022 14:25:15 +0100 Subject: [PATCH 10/43] remove into(). in doc examples, minor doc changes --- primitives/arithmetic/src/traits.rs | 64 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 519146439dcdd..0b7849d7e5a06 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -364,8 +364,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_add(self, v: Self) -> Result { self.checked_add(&v).ok_or_else(|| error::equivalent(v)) @@ -392,8 +392,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` fn ensure_sub(self, v: Self) -> Result { self.checked_sub(&v).ok_or_else(|| error::inverse(v)) @@ -421,8 +421,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_mul(self, v: Self) -> Result { self.checked_mul(&v).ok_or_else(|| error::multiplication(self, v)) @@ -437,7 +437,7 @@ mod ensure { /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error /// /// ``` - /// use sp_arithmetic::{traits::EnsureDiv, ArithmeticError, FixedI64}; + /// use sp_arithmetic::{traits::EnsureDiv, ArithmeticError}; /// /// fn extrinsic_zero() -> Result<(), ArithmeticError> { /// 1.ensure_div(0)?; @@ -445,12 +445,12 @@ mod ensure { /// } /// /// fn overflow() -> Result<(), ArithmeticError> { - /// FixedI64::from(i64::MIN).ensure_div(FixedI64::from(-1))?; + /// i64::MIN.ensure_div(-1)?; /// Ok(()) /// } /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero)); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` fn ensure_div(self, v: Self) -> Result { self.checked_div(&v).ok_or_else(|| error::division(self, v)) @@ -462,7 +462,7 @@ mod ensure { impl EnsureMul for T {} impl EnsureDiv for T {} - /// Meta trait that supports all inmutable arithmetic operations + /// Meta trait that supports all immutable arithmetic `Ensure*` operations pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} impl EnsureOp for T {} @@ -487,8 +487,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_add(v)?; @@ -517,8 +517,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_sub(v)?; @@ -547,8 +547,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_mul(v)?; @@ -577,8 +577,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero)); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { *self = self.ensure_div(v)?; @@ -591,7 +591,7 @@ mod ensure { impl EnsureMulAssign for T {} impl EnsureDivAssign for T {} - /// Meta trait that supports all assigned arithmetic operations + /// Meta trait that supports all assigned arithmetic `Ensure*` operations pub trait EnsureOpAssign: EnsureAddAssign + EnsureSubAssign + EnsureMulAssign + EnsureDivAssign { @@ -605,7 +605,7 @@ mod ensure { pub trait Ensure: EnsureOp + EnsureOpAssign {} impl Ensure for T {} - /// Extends `FixedPointNumber with` the Ensure family functions. + /// Extends `FixedPointNumber` with the Ensure family functions. pub trait EnsureFixedPointNumber: FixedPointNumber { /// Creates `self` from a rational number. Equal to `n / d`. /// @@ -627,8 +627,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_from_rational( n: N, @@ -642,7 +642,7 @@ mod ensure { /// /// Returns `ArithmeticError` if the result does not fit in `N`. /// - /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an ArithmeticError + /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an `ArithmeticError` /// error /// /// ``` @@ -658,8 +658,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_mul_int(self, n: N) -> Result { self.checked_mul_int(n).ok_or_else(|| error::multiplication(self, n)) @@ -685,8 +685,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero.into())); - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); + /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero)); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` fn ensure_div_int(self, d: D) -> Result { self.checked_div_int(d).ok_or_else(|| error::division(self, d)) @@ -716,8 +716,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_from(other: T) -> Result { Self::try_from(other).map_err(|_| error::equivalent(other)) @@ -745,8 +745,8 @@ mod ensure { /// Ok(()) /// } /// - /// assert_eq!(overflow(), Err(ArithmeticError::Overflow.into())); - /// assert_eq!(underflow(), Err(ArithmeticError::Underflow.into())); + /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); + /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` fn ensure_into(self) -> Result { self.try_into().map_err(|_| error::equivalent(self)) From f7b305ccb82309373ce5bfcdddc983e9d7d5673f Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 23 Dec 2022 09:15:19 +0100 Subject: [PATCH 11/43] remove return value from assign methods --- primitives/arithmetic/src/traits.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 0b7849d7e5a06..bdf5f514d8810 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -490,9 +490,9 @@ mod ensure { /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` - fn ensure_add_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + fn ensure_add_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { *self = self.ensure_add(v)?; - Ok(self) + Ok(()) } } @@ -520,9 +520,9 @@ mod ensure { /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` - fn ensure_sub_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + fn ensure_sub_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { *self = self.ensure_sub(v)?; - Ok(self) + Ok(()) } } @@ -550,9 +550,9 @@ mod ensure { /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); /// ``` - fn ensure_mul_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + fn ensure_mul_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { *self = self.ensure_mul(v)?; - Ok(self) + Ok(()) } } @@ -580,9 +580,9 @@ mod ensure { /// assert_eq!(extrinsic_zero(), Err(ArithmeticError::DivisionByZero)); /// assert_eq!(overflow(), Err(ArithmeticError::Overflow)); /// ``` - fn ensure_div_assign(&mut self, v: Self) -> Result<&mut Self, ArithmeticError> { + fn ensure_div_assign(&mut self, v: Self) -> Result<(), ArithmeticError> { *self = self.ensure_div(v)?; - Ok(self) + Ok(()) } } From 18857bc960690c8419f23f70e6ddc0e5652314ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:05:21 +0100 Subject: [PATCH 12/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index bdf5f514d8810..7e0cab971043b 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -347,7 +347,8 @@ mod ensure { /// Performs addition that returns `ArithmeticError` instead of wrapping around on overflow. pub trait EnsureAdd: CheckedAdd + PartialOrd + Zero + Copy { /// Adds two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error /// From df6e94fa205ba2880f85aa9fefbc1afeb13d980e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:05:42 +0100 Subject: [PATCH 13/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 7e0cab971043b..68d3685687b99 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -344,7 +344,7 @@ mod ensure { use super::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}; use crate::{ArithmeticError, FixedPointNumber, FixedPointOperand}; - /// Performs addition that returns `ArithmeticError` instead of wrapping around on overflow. + /// Performs addition that returns [`ArithmeticError`] instead of wrapping around on overflow. pub trait EnsureAdd: CheckedAdd + PartialOrd + Zero + Copy { /// Adds two numbers, checking for overflow. /// From 240067be06643e43664ada20738dcb07ecf63e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:06:47 +0100 Subject: [PATCH 14/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 68d3685687b99..e9e6fa6376856 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -668,10 +668,10 @@ mod ensure { /// Ensure division for integer type `N`. Equal to `self / d`. /// - /// Returns `ArithmeticError` if the result does not fit in `N` or `d == 0`. + /// Returns [`ArithmeticError`] if the result does not fit in `N` or `d == 0`. /// - /// Similar to [`FixedPointNumber::checked_div_int()`] but returning an `ArithmeticError` - /// error + /// Similar to [`FixedPointNumber::checked_div_int()`] but returning an [`ArithmeticError`] + /// error. /// /// ``` /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; From 95b386e15666ce9013a8ada94fc0b6af5c7320b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:07:00 +0100 Subject: [PATCH 15/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index e9e6fa6376856..d7f9e09d9aee1 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -696,7 +696,7 @@ mod ensure { impl EnsureFixedPointNumber for T {} - /// Similar to [`TryFrom`] but returning an `ArithmeticError` error. + /// Similar to [`TryFrom`] but returning an [`ArithmeticError`] error. pub trait EnsureFrom: TryFrom + PartialOrd + Zero + Copy { From f9384d6d4c71cc85da7070a4d3351b9b1bf9b081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:07:11 +0100 Subject: [PATCH 16/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index d7f9e09d9aee1..9ef00b7cc11e3 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -729,9 +729,9 @@ mod ensure { pub trait EnsureInto: TryInto + PartialOrd + Zero + Copy { - /// Performs the conversion returning an `ArithmeticError` if fails. + /// Performs the conversion returning an [`ArithmeticError`] if fails. /// - /// Similar to [`TryInto::try_into()`] but returning an `ArithmeticError` error + /// Similar to [`TryInto::try_into()`] but returning an [`ArithmeticError`] error /// /// ``` /// use sp_arithmetic::{traits::EnsureInto, ArithmeticError}; From 2a72a0d8ed25bf726119b9d0fc3a154dd7572288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:07:42 +0100 Subject: [PATCH 17/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 9ef00b7cc11e3..2cf4bf04f233b 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -725,7 +725,7 @@ mod ensure { } } - /// Similar to [`TryInto`] but returning an `ArithmeticError` error. + /// Similar to [`TryInto`] but returning an [`ArithmeticError`] error. pub trait EnsureInto: TryInto + PartialOrd + Zero + Copy { From 8eaffcc0fc646e11c4bd6d67cd6e81e3e9d6f7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:08:03 +0100 Subject: [PATCH 18/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 2cf4bf04f233b..02d52df29c188 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -700,9 +700,9 @@ mod ensure { pub trait EnsureFrom: TryFrom + PartialOrd + Zero + Copy { - /// Performs the conversion returning an `ArithmeticError` if fails. + /// Performs the conversion returning an [`ArithmeticError`] if fails. /// - /// Similar to [`TryFrom::try_from()`] but returning an `ArithmeticError` error + /// Similar to [`TryFrom::try_from()`] but returning an [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureFrom, ArithmeticError}; From 5870a3fc0c89f19bdb3f5f8a6951a37184de6ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:08:25 +0100 Subject: [PATCH 19/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 02d52df29c188..5ee543c60e7d0 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -350,7 +350,7 @@ mod ensure { /// /// If it fails, [`ArithmeticError`] is returned. /// - /// Similar to [`CheckedAdd::checked_add()`] but returning an `ArithmeticError` error + /// Similar to [`CheckedAdd::checked_add()`] but returning an [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureAdd, ArithmeticError}; From ac7bc371181939762317d40d8e98a8d5b844816b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:08:42 +0100 Subject: [PATCH 20/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 5ee543c60e7d0..1a31e490553b3 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -373,7 +373,7 @@ mod ensure { } } - /// Performs subtraction that returns `ArithmeticError` instead of wrapping around on underflow. + /// Performs subtraction that returns [`ArithmeticError`] instead of wrapping around on underflow. pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { /// Subtracts two numbers, checking for overflow. /// If overflow happens, `ArithmeticError` is returned. From 274f4cd2cfa1f3eccfdaa25a900b2d404df14637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:09:06 +0100 Subject: [PATCH 21/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 1a31e490553b3..0963c9960780b 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -376,7 +376,8 @@ mod ensure { /// Performs subtraction that returns [`ArithmeticError`] instead of wrapping around on underflow. pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { /// Subtracts two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error /// From 8cd046012f672541e21252d21623e02baf96d0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:09:50 +0100 Subject: [PATCH 22/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 0963c9960780b..c57bcc9f2954e 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -379,7 +379,7 @@ mod ensure { /// /// If it fails, [`ArithmeticError`] is returned. /// - /// Similar to [`CheckedSub::checked_sub()`] but returning an `ArithmeticError` error + /// Similar to [`CheckedSub::checked_sub()`] but returning an [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureSub, ArithmeticError}; From 4b4431c0d74cf7aa93a220a2246d3d80d65c4593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:10:12 +0100 Subject: [PATCH 23/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index c57bcc9f2954e..a94d154694d44 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -402,7 +402,7 @@ mod ensure { } } - /// Performs multiplication that returns `ArithmeticError` instead of wrapping around on + /// Performs multiplication that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { /// Multiplies two numbers, checking for overflow. If overflow happens, From c8c46fa1e0f0f863349423c148033c375fee9bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:10:36 +0100 Subject: [PATCH 24/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index a94d154694d44..40ba40013c291 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -405,8 +405,9 @@ mod ensure { /// Performs multiplication that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { - /// Multiplies two numbers, checking for overflow. If overflow happens, - /// `ArithmeticError` is returned. + /// Multiplies two numbers, checking for overflow. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error /// From 1e2471cf5052b7a385989f3862591638df9fcb87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:28:47 +0100 Subject: [PATCH 25/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 40ba40013c291..1dfdcff7e7b77 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -409,7 +409,7 @@ mod ensure { /// /// If it fails, [`ArithmeticError`] is returned. /// - /// Similar to [`CheckedMul::checked_mul()`] but returning an `ArithmeticError` error + /// Similar to [`CheckedMul::checked_mul()`] but returning an [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureMul, ArithmeticError}; From 2b432719362acde487647aa4591bb7fbdfd78d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:29:05 +0100 Subject: [PATCH 26/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 1dfdcff7e7b77..cc947a168e9a7 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -432,7 +432,7 @@ mod ensure { } } - /// Performs division that returns `ArithmeticError` instead of wrapping around on overflow. + /// Performs division that returns [`ArithmeticError`] instead of wrapping around on overflow. pub trait EnsureDiv: CheckedDiv + PartialOrd + Zero + Copy { /// Divides two numbers, checking for overflow. /// If overflow happens, `ArithmeticError` is returned. From ea54b663276fecd30fdc767f705af3f18aa654c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:49:16 +0100 Subject: [PATCH 27/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index cc947a168e9a7..63d0cdf9a4004 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -435,7 +435,8 @@ mod ensure { /// Performs division that returns [`ArithmeticError`] instead of wrapping around on overflow. pub trait EnsureDiv: CheckedDiv + PartialOrd + Zero + Copy { /// Divides two numbers, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error /// From e12cd9d0672bda756c833e5efc750b4409aa616d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:49:34 +0100 Subject: [PATCH 28/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 63d0cdf9a4004..abe0f21a90816 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -438,7 +438,7 @@ mod ensure { /// /// If it fails, [`ArithmeticError`] is returned. /// - /// Similar to [`CheckedDiv::checked_div()`] but returning an `ArithmeticError` error + /// Similar to [`CheckedDiv::checked_div()`] but returning an [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureDiv, ArithmeticError}; From 9b368391bef0a8154cf975605f96952babdfafd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:50:18 +0100 Subject: [PATCH 29/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index abe0f21a90816..ced6287f5014b 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -470,7 +470,7 @@ mod ensure { pub trait EnsureOp: EnsureAdd + EnsureSub + EnsureMul + EnsureDiv {} impl EnsureOp for T {} - /// Performs self addition that returns `ArithmeticError` instead of wrapping around on + /// Performs self addition that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureAddAssign: EnsureAdd { /// Adds two numbers overwriting the left hand one, checking for overflow. From 5435fe40c03aef73b8699980ba60cf68f42bebd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 11:50:48 +0100 Subject: [PATCH 30/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index ced6287f5014b..47436167a929b 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -474,7 +474,8 @@ mod ensure { /// overflow. pub trait EnsureAddAssign: EnsureAdd { /// Adds two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// ``` /// use sp_arithmetic::{traits::EnsureAddAssign, ArithmeticError}; From cb0202ecaf6ce9fde8ed56bac28a73fd2ab1ca75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:01:02 +0100 Subject: [PATCH 31/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 47436167a929b..704571c9644aa 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -501,7 +501,7 @@ mod ensure { } } - /// Performs self subtraction that returns `ArithmeticError` instead of wrapping around on + /// Performs self subtraction that returns [`ArithmeticError`] instead of wrapping around on /// underflow. pub trait EnsureSubAssign: EnsureSub { /// Subtracts two numbers overwriting the left hand one, checking for overflow. From d9fa4e1f090f114e4349d7ebc1d695088d84746c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:01:23 +0100 Subject: [PATCH 32/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 704571c9644aa..bbbbccc5ba3f6 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -505,7 +505,8 @@ mod ensure { /// underflow. pub trait EnsureSubAssign: EnsureSub { /// Subtracts two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// ``` /// use sp_arithmetic::{traits::EnsureSubAssign, ArithmeticError}; From 3640a6fc5f8c9299f3cd16faa426c366218997f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:02:38 +0100 Subject: [PATCH 33/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index bbbbccc5ba3f6..adf9e5370ff59 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -532,7 +532,7 @@ mod ensure { } } - /// Performs self multiplication that returns `ArithmeticError` instead of wrapping around on + /// Performs self multiplication that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureMulAssign: EnsureMul { /// Multiplies two numbers overwriting the left hand one, checking for overflow. From 5f59e68d39a9b98a42d15c8fdc125249173d923d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:02:57 +0100 Subject: [PATCH 34/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index adf9e5370ff59..eaa83d892bd19 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -536,7 +536,8 @@ mod ensure { /// overflow. pub trait EnsureMulAssign: EnsureMul { /// Multiplies two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// ``` /// use sp_arithmetic::{traits::EnsureMulAssign, ArithmeticError}; From 0e6a2a61f9f049b4d6169df858aaaee17cc4e920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:03:15 +0100 Subject: [PATCH 35/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index eaa83d892bd19..d7e728756edc6 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -563,7 +563,7 @@ mod ensure { } } - /// Performs self division that returns `ArithmeticError` instead of wrapping around on + /// Performs self division that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureDivAssign: EnsureDiv { /// Divides two numbers overwriting the left hand one, checking for overflow. From d44e76cfde341aa984910db6c31d918c30bc2199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:03:31 +0100 Subject: [PATCH 36/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index d7e728756edc6..5f0d2a54d736f 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -567,7 +567,8 @@ mod ensure { /// overflow. pub trait EnsureDivAssign: EnsureDiv { /// Divides two numbers overwriting the left hand one, checking for overflow. - /// If overflow happens, `ArithmeticError` is returned. + /// + /// If it fails, [`ArithmeticError`] is returned. /// /// ``` /// use sp_arithmetic::{traits::EnsureDivAssign, ArithmeticError, FixedI64}; From 784a80348929e7ed28f4aabaa7725e1db47f4c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:03:46 +0100 Subject: [PATCH 37/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 5f0d2a54d736f..d4c4d8d3a0ea0 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -613,7 +613,7 @@ mod ensure { pub trait Ensure: EnsureOp + EnsureOpAssign {} impl Ensure for T {} - /// Extends `FixedPointNumber` with the Ensure family functions. + /// Extends [`FixedPointNumber`] with the Ensure family functions. pub trait EnsureFixedPointNumber: FixedPointNumber { /// Creates `self` from a rational number. Equal to `n / d`. /// From 48e68c5f63e356e6987f599c1fb8849f1757d616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:04:21 +0100 Subject: [PATCH 38/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index d4c4d8d3a0ea0..84d8b9ef33bdd 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -617,7 +617,7 @@ mod ensure { pub trait EnsureFixedPointNumber: FixedPointNumber { /// Creates `self` from a rational number. Equal to `n / d`. /// - /// Returns `ArithmeticError` if `d == 0` or `n / d` exceeds accuracy. + /// Returns [`ArithmeticError`] if `d == 0` or `n / d` exceeds accuracy. /// /// Similar to [`FixedPointNumber::checked_from_rational()`] but returning an /// `ArithmeticError` error From 2a2590bf4d1a036c5599eb803338ba391457a0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:04:41 +0100 Subject: [PATCH 39/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 84d8b9ef33bdd..c8b884bd8c0d5 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -620,7 +620,7 @@ mod ensure { /// Returns [`ArithmeticError`] if `d == 0` or `n / d` exceeds accuracy. /// /// Similar to [`FixedPointNumber::checked_from_rational()`] but returning an - /// `ArithmeticError` error + /// [`ArithmeticError`] error. /// /// ``` /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; From b23f70f72442022b3ef2c0f2658d4daf9f2159b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 27 Dec 2022 12:04:57 +0100 Subject: [PATCH 40/43] Update primitives/arithmetic/src/traits.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- primitives/arithmetic/src/traits.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index c8b884bd8c0d5..219d5410db57e 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -648,10 +648,10 @@ mod ensure { /// Ensure multiplication for integer type `N`. Equal to `self * n`. /// - /// Returns `ArithmeticError` if the result does not fit in `N`. + /// Returns [`ArithmeticError`] if the result does not fit in `N`. /// - /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an `ArithmeticError` - /// error + /// Similar to [`FixedPointNumber::checked_mul_int()`] but returning an [`ArithmeticError`] + /// error. /// /// ``` /// use sp_arithmetic::{traits::EnsureFixedPointNumber, ArithmeticError, FixedI64}; From b0219f53fa5366885c96122174efdc7c35115939 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Tue, 27 Dec 2022 14:09:28 +0100 Subject: [PATCH 41/43] cargo fmt --- primitives/arithmetic/src/traits.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 219d5410db57e..73ec5e870dcec 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -373,7 +373,8 @@ mod ensure { } } - /// Performs subtraction that returns [`ArithmeticError`] instead of wrapping around on underflow. + /// Performs subtraction that returns [`ArithmeticError`] instead of wrapping around on + /// underflow. pub trait EnsureSub: CheckedSub + PartialOrd + Zero + Copy { /// Subtracts two numbers, checking for overflow. /// @@ -405,7 +406,7 @@ mod ensure { /// Performs multiplication that returns [`ArithmeticError`] instead of wrapping around on /// overflow. pub trait EnsureMul: CheckedMul + PartialOrd + Zero + Copy { - /// Multiplies two numbers, checking for overflow. + /// Multiplies two numbers, checking for overflow. /// /// If it fails, [`ArithmeticError`] is returned. /// From 9ba930792734e3f2262d1f6adc4debcc410bdf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 28 Dec 2022 11:14:57 +0100 Subject: [PATCH 42/43] Apply suggestions from code review --- primitives/arithmetic/src/traits.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 73ec5e870dcec..1af32907b0938 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -334,12 +334,10 @@ impl SaturatedConversion for T {} /// # } /// ``` /// -/// choosing the correct [`ArithmeticError`] it should return in case of fail. +/// choosing the correct [`ArithmeticError`](crate::ArithmeticError) it should return in case of fail. /// /// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but -/// returning an [`ArithmeticError`] instead of `None`. -/// -/// [`ArithmeticError`]: crate::ArithmeticError +/// returning an [`ArithmeticError`](crate::ArithmeticError) instead of `None`. mod ensure { use super::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Zero}; use crate::{ArithmeticError, FixedPointNumber, FixedPointOperand}; From 048a34b8daa75730b19f8bf553fcb993f7eb6485 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Wed, 28 Dec 2022 10:21:43 +0000 Subject: [PATCH 43/43] ".git/.scripts/fmt.sh" 1 --- primitives/arithmetic/src/traits.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/arithmetic/src/traits.rs b/primitives/arithmetic/src/traits.rs index 1af32907b0938..7fa64d28669d4 100644 --- a/primitives/arithmetic/src/traits.rs +++ b/primitives/arithmetic/src/traits.rs @@ -334,7 +334,8 @@ impl SaturatedConversion for T {} /// # } /// ``` /// -/// choosing the correct [`ArithmeticError`](crate::ArithmeticError) it should return in case of fail. +/// choosing the correct [`ArithmeticError`](crate::ArithmeticError) it should return in case of +/// fail. /// /// The *EnsureOps* family functions follows the same behavior as *CheckedOps* but /// returning an [`ArithmeticError`](crate::ArithmeticError) instead of `None`.