Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Runtime: Add common morph utility types (#14281)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork authored Jun 1, 2023
1 parent 286a681 commit f50c501
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 12 additions & 0 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f50c501

Please sign in to comment.