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

Runtime: Add common morph utility types #14281

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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