Skip to content

Commit

Permalink
Add ensure-ops family methods (paritytech#12967)
Browse files Browse the repository at this point in the history
* add ensure-ops family methods

* fix cargo doc

* add EnsureOp and EnsureOpAssign meta traits

* move ensure module and ArithmeticError to sp-arithmetic

* fix doc examples

* reexport ensure module content

* ensure mod private

* reexport to sp-runtime

* fix doc example

* remove into(). in doc examples, minor doc changes

* remove return value from assign methods

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <[email protected]>

* cargo fmt

* Apply suggestions from code review

* ".git/.scripts/fmt.sh" 1

Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
2 people authored and ltfschoen committed Feb 22, 2023
1 parent 28ace21 commit 078a042
Show file tree
Hide file tree
Showing 4 changed files with 564 additions and 27 deletions.
28 changes: 28 additions & 0 deletions primitives/arithmetic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArithmeticError> 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:
Expand Down
Loading

0 comments on commit 078a042

Please sign in to comment.