Remove u32_trait (#10850)

* Remove `u32_trait`

This trait only existed because there wasn't any const generic support at time of creation. However,
we now have support for it :)

* FMT
This commit is contained in:
Bastian Köcher
2022-02-14 21:12:25 +01:00
committed by GitHub
parent 888131b651
commit fdd49f072c
6 changed files with 39 additions and 620 deletions
+17 -18
View File
@@ -43,7 +43,6 @@
#![recursion_limit = "128"]
use scale_info::TypeInfo;
use sp_core::u32_trait::Value as U32;
use sp_io::storage;
use sp_runtime::{traits::Hash, RuntimeDebug};
use sp_std::{marker::PhantomData, prelude::*, result};
@@ -1011,43 +1010,43 @@ impl<
}
}
pub struct EnsureMembers<N: U32, AccountId, I: 'static>(PhantomData<(N, AccountId, I)>);
pub struct EnsureMembers<AccountId, I: 'static, const N: u32>(PhantomData<(AccountId, I)>);
impl<
O: Into<Result<RawOrigin<AccountId, I>, O>> + From<RawOrigin<AccountId, I>>,
N: U32,
AccountId,
I,
> EnsureOrigin<O> for EnsureMembers<N, AccountId, I>
const N: u32,
> EnsureOrigin<O> for EnsureMembers<AccountId, I, N>
{
type Success = (MemberCount, MemberCount);
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
RawOrigin::Members(n, m) if n >= N::VALUE => Ok((n, m)),
RawOrigin::Members(n, m) if n >= N => Ok((n, m)),
r => Err(O::from(r)),
})
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
O::from(RawOrigin::Members(N::VALUE, N::VALUE))
O::from(RawOrigin::Members(N, N))
}
}
pub struct EnsureProportionMoreThan<N: U32, D: U32, AccountId, I: 'static>(
PhantomData<(N, D, AccountId, I)>,
pub struct EnsureProportionMoreThan<AccountId, I: 'static, const N: u32, const D: u32>(
PhantomData<(AccountId, I)>,
);
impl<
O: Into<Result<RawOrigin<AccountId, I>, O>> + From<RawOrigin<AccountId, I>>,
N: U32,
D: U32,
AccountId,
I,
> EnsureOrigin<O> for EnsureProportionMoreThan<N, D, AccountId, I>
const N: u32,
const D: u32,
> EnsureOrigin<O> for EnsureProportionMoreThan<AccountId, I, N, D>
{
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
RawOrigin::Members(n, m) if n * D::VALUE > N::VALUE * m => Ok(()),
RawOrigin::Members(n, m) if n * D > N * m => Ok(()),
r => Err(O::from(r)),
})
}
@@ -1058,21 +1057,21 @@ impl<
}
}
pub struct EnsureProportionAtLeast<N: U32, D: U32, AccountId, I: 'static>(
PhantomData<(N, D, AccountId, I)>,
pub struct EnsureProportionAtLeast<AccountId, I: 'static, const N: u32, const D: u32>(
PhantomData<(AccountId, I)>,
);
impl<
O: Into<Result<RawOrigin<AccountId, I>, O>> + From<RawOrigin<AccountId, I>>,
N: U32,
D: U32,
AccountId,
I,
> EnsureOrigin<O> for EnsureProportionAtLeast<N, D, AccountId, I>
const N: u32,
const D: u32,
> EnsureOrigin<O> for EnsureProportionAtLeast<AccountId, I, N, D>
{
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
RawOrigin::Members(n, m) if n * D::VALUE >= N::VALUE * m => Ok(()),
RawOrigin::Members(n, m) if n * D >= N * m => Ok(()),
r => Err(O::from(r)),
})
}
+2 -5
View File
@@ -24,10 +24,7 @@ use frame_support::{
Hashable,
};
use frame_system::{EventRecord, Phase};
use sp_core::{
u32_trait::{_3, _4},
H256,
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
@@ -142,7 +139,7 @@ impl Config<Instance2> for Test {
}
impl mock_democracy::Config for Test {
type Event = Event;
type ExternalMajorityOrigin = EnsureProportionAtLeast<_3, _4, u64, Instance1>;
type ExternalMajorityOrigin = EnsureProportionAtLeast<u64, Instance1, 3, 4>;
}
impl Config for Test {
type Origin = Origin;
@@ -18,27 +18,26 @@
//! Means for splitting an imbalance into two and hanlding them differently.
use super::super::imbalance::{Imbalance, OnUnbalanced};
use sp_core::u32_trait::Value as U32;
use sp_runtime::traits::Saturating;
use sp_std::{marker::PhantomData, ops::Div};
/// Split an unbalanced amount two ways between a common divisor.
pub struct SplitTwoWays<Balance, Imbalance, Part1, Target1, Part2, Target2>(
PhantomData<(Balance, Imbalance, Part1, Target1, Part2, Target2)>,
pub struct SplitTwoWays<Balance, Imbalance, Target1, Target2, const PART1: u32, const PART2: u32>(
PhantomData<(Balance, Imbalance, Target1, Target2)>,
);
impl<
Balance: From<u32> + Saturating + Div<Output = Balance>,
I: Imbalance<Balance>,
Part1: U32,
Target1: OnUnbalanced<I>,
Part2: U32,
Target2: OnUnbalanced<I>,
> OnUnbalanced<I> for SplitTwoWays<Balance, I, Part1, Target1, Part2, Target2>
const PART1: u32,
const PART2: u32,
> OnUnbalanced<I> for SplitTwoWays<Balance, I, Target1, Target2, PART1, PART2>
{
fn on_nonzero_unbalanced(amount: I) {
let total: u32 = Part1::VALUE + Part2::VALUE;
let amount1 = amount.peek().saturating_mul(Part1::VALUE.into()) / total.into();
let total: u32 = PART1 + PART2;
let amount1 = amount.peek().saturating_mul(PART1.into()) / total.into();
let (imb1, imb2) = amount.split(amount1);
Target1::on_unbalanced(imb1);
Target2::on_unbalanced(imb2);