Skip to content

Commit 06f8647

Browse files
authored
Helper macro for Morph impls (paritytech#11570)
* Helper macro for Morph impls * No need to deprecate for now * Improved macro * Doc tests * Grumbles
1 parent 1b3ddae commit 06f8647

File tree

5 files changed

+376
-95
lines changed

5 files changed

+376
-95
lines changed

frame/ranked-collective/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use frame_support::{
2323
assert_noop, assert_ok,
2424
error::BadOrigin,
2525
parameter_types,
26-
traits::{ConstU16, ConstU32, ConstU64, EitherOf, Everything, MapSuccess, Polling, ReduceBy},
26+
traits::{ConstU16, ConstU32, ConstU64, EitherOf, Everything, MapSuccess, Polling},
2727
};
2828
use sp_core::H256;
2929
use sp_runtime::{
3030
testing::Header,
31-
traits::{BlakeTwo256, Identity, IdentityLookup},
31+
traits::{BlakeTwo256, Identity, IdentityLookup, ReduceBy},
3232
};
3333

3434
use super::*;

frame/support/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod dispatch;
9797
pub use dispatch::EnsureOneOf;
9898
pub use dispatch::{
9999
AsEnsureOriginWithArg, DispatchableWithStorageLayer, EitherOf, EitherOfDiverse, EnsureOrigin,
100-
EnsureOriginWithArg, MapSuccess, NeverEnsureOrigin, OriginTrait, ReduceBy, TryMapSuccess,
100+
EnsureOriginWithArg, MapSuccess, NeverEnsureOrigin, OriginTrait, TryMapSuccess,
101101
UnfilteredDispatchable,
102102
};
103103

frame/support/src/traits/dispatch.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
//! Traits for dealing with dispatching calls and the origin from which they are dispatched.
1919
2020
use crate::dispatch::{DispatchResultWithPostInfo, Parameter, RawOrigin};
21-
use sp_arithmetic::traits::{CheckedSub, Zero};
2221
use sp_runtime::{
2322
traits::{BadOrigin, Member, Morph, TryMorph},
2423
Either,
2524
};
2625
use sp_std::marker::PhantomData;
2726

28-
use super::TypedGet;
29-
3027
/// Some sort of check on the origin is performed by this object.
3128
pub trait EnsureOrigin<OuterOrigin> {
3229
/// A return type.
@@ -226,27 +223,6 @@ impl<
226223
}
227224
}
228225

229-
/// Mutator which reduces a scalar by a particular amount.
230-
pub struct ReduceBy<N>(PhantomData<N>);
231-
impl<N: TypedGet> TryMorph<N::Type> for ReduceBy<N>
232-
where
233-
N::Type: CheckedSub,
234-
{
235-
type Outcome = N::Type;
236-
fn try_morph(r: N::Type) -> Result<N::Type, ()> {
237-
r.checked_sub(&N::get()).ok_or(())
238-
}
239-
}
240-
impl<N: TypedGet> Morph<N::Type> for ReduceBy<N>
241-
where
242-
N::Type: CheckedSub + Zero,
243-
{
244-
type Outcome = N::Type;
245-
fn morph(r: N::Type) -> N::Type {
246-
r.checked_sub(&N::get()).unwrap_or(Zero::zero())
247-
}
248-
}
249-
250226
/// Type that can be dispatched with an origin but without checking the origin filter.
251227
///
252228
/// Implemented for pallet dispatchable type by `decl_module` and for runtime dispatchable by

frame/support/src/traits/misc.rs

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ use crate::dispatch::Parameter;
2121
use codec::{CompactLen, Decode, DecodeAll, Encode, EncodeLike, Input, MaxEncodedLen};
2222
use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
2323
use sp_arithmetic::traits::{CheckedAdd, CheckedMul, CheckedSub, Saturating};
24+
#[doc(hidden)]
25+
pub use sp_runtime::traits::{
26+
ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128, ConstU16, ConstU32,
27+
ConstU64, ConstU8, Get, GetDefault, TypedGet,
28+
};
2429
use sp_runtime::{traits::Block as BlockT, DispatchError};
2530
use sp_std::{cmp::Ordering, prelude::*};
2631

@@ -387,73 +392,6 @@ where
387392
}
388393
}
389394

390-
/// A trait for querying a single value from a type defined in the trait.
391-
///
392-
/// It is not required that the value is constant.
393-
pub trait TypedGet {
394-
/// The type which is returned.
395-
type Type;
396-
/// Return the current value.
397-
fn get() -> Self::Type;
398-
}
399-
400-
/// A trait for querying a single value from a type.
401-
///
402-
/// It is not required that the value is constant.
403-
pub trait Get<T> {
404-
/// Return the current value.
405-
fn get() -> T;
406-
}
407-
408-
impl<T: Default> Get<T> for () {
409-
fn get() -> T {
410-
T::default()
411-
}
412-
}
413-
414-
/// Implement Get by returning Default for any type that implements Default.
415-
pub struct GetDefault;
416-
impl<T: Default> Get<T> for GetDefault {
417-
fn get() -> T {
418-
T::default()
419-
}
420-
}
421-
422-
macro_rules! impl_const_get {
423-
($name:ident, $t:ty) => {
424-
#[derive($crate::RuntimeDebug)]
425-
pub struct $name<const T: $t>;
426-
impl<const T: $t> Get<$t> for $name<T> {
427-
fn get() -> $t {
428-
T
429-
}
430-
}
431-
impl<const T: $t> Get<Option<$t>> for $name<T> {
432-
fn get() -> Option<$t> {
433-
Some(T)
434-
}
435-
}
436-
impl<const T: $t> TypedGet for $name<T> {
437-
type Type = $t;
438-
fn get() -> $t {
439-
T
440-
}
441-
}
442-
};
443-
}
444-
445-
impl_const_get!(ConstBool, bool);
446-
impl_const_get!(ConstU8, u8);
447-
impl_const_get!(ConstU16, u16);
448-
impl_const_get!(ConstU32, u32);
449-
impl_const_get!(ConstU64, u64);
450-
impl_const_get!(ConstU128, u128);
451-
impl_const_get!(ConstI8, i8);
452-
impl_const_get!(ConstI16, i16);
453-
impl_const_get!(ConstI32, i32);
454-
impl_const_get!(ConstI64, i64);
455-
impl_const_get!(ConstI128, i128);
456-
457395
/// A type for which some values make sense to be able to drop without further consideration.
458396
pub trait TryDrop: Sized {
459397
/// Drop an instance cleanly. Only works if its value represents "no-operation".

0 commit comments

Comments
 (0)