From f50c501411d2e453eb5d10d5e8ee73194d7f7340 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Thu, 1 Jun 2023 17:11:53 +0100 Subject: [PATCH] Runtime: Add common morph utility types (#14281) --- frame/core-fellowship/src/lib.rs | 2 +- primitives/runtime/src/traits.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/frame/core-fellowship/src/lib.rs b/frame/core-fellowship/src/lib.rs index 97603fcc6ce9e..9ca13db172638 100644 --- a/frame/core-fellowship/src/lib.rs +++ b/frame/core-fellowship/src/lib.rs @@ -321,7 +321,7 @@ pub mod pallet { /// Set the parameters. /// - /// - `origin`: A origin complying with `ParamsOrigin` or root. + /// - `origin`: An origin complying with `ParamsOrigin` or root. /// - `params`: The new parameters for the pallet. #[pallet::weight(T::WeightInfo::set_params())] #[pallet::call_index(1)] diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index a1874bcecf3c5..78f2e393014ce 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -512,10 +512,22 @@ macro_rules! morph_types { morph_types! { /// Morpher to disregard the source value and replace with another. pub type Replace = |_| -> V::Type { V::get() }; + /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type { r.checked_sub(&N::get()).unwrap_or(Zero::zero()) } where N::Type: CheckedSub | Zero; + + /// A `TryMorph` implementation to reduce a scalar by a particular amount, checking for + /// underflow. + pub type CheckedReduceBy: TryMorph = |r: N::Type| -> Result { + r.checked_sub(&N::get()).ok_or(()) + } where N::Type: CheckedSub; + + /// A `TryMorph` implementation to enforce an upper limit for a result of the outer morphed type. + pub type MorphWithUpperLimit: TryMorph = |r: L::Type| -> Result { + M::try_morph(r).map(|m| m.min(L::get())) + } where L::Type: Ord, M: TryMorph; } /// Extensible conversion trait. Generic over both source and destination types.