Runtime: Add common morph utility types (#14281)

This commit is contained in:
Gavin Wood
2023-06-01 17:11:53 +01:00
committed by GitHub
parent 0910390b25
commit feb0f45d21
2 changed files with 13 additions and 1 deletions
+1 -1
View File
@@ -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)]
@@ -512,10 +512,22 @@ macro_rules! morph_types {
morph_types! {
/// Morpher to disregard the source value and replace with another.
pub type Replace<V: TypedGet> = |_| -> V::Type { V::get() };
/// Mutator which reduces a scalar by a particular amount.
pub type ReduceBy<N: TypedGet> = |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<N: TypedGet>: TryMorph = |r: N::Type| -> Result<N::Type, ()> {
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<L: TypedGet, M>: TryMorph = |r: L::Type| -> Result<L::Type, ()> {
M::try_morph(r).map(|m| m.min(L::get()))
} where L::Type: Ord, M: TryMorph<L::Type, Outcome = L::Type>;
}
/// Extensible conversion trait. Generic over both source and destination types.