Attempt to remove the where bounds in arithmetic. (#7933)

* Attempt to remove the where bounds.

* Fix further and further.

* Format better.

* Update primitives/npos-elections/src/lib.rs

* fix build

* remove unused
This commit is contained in:
Kian Paimani
2021-01-21 09:39:30 +00:00
committed by GitHub
parent 9031c35114
commit 0dd8cd6277
11 changed files with 125 additions and 139 deletions
@@ -17,11 +17,8 @@
//! Helper methods for npos-elections.
use crate::{
Assignment, Error, ExtendedBalance, IdentifierT, PerThing128, StakedAssignment, VoteWeight,
WithApprovalOf,
};
use sp_arithmetic::{InnerOf, PerThing};
use crate::{Assignment, Error, IdentifierT, PerThing128, StakedAssignment, VoteWeight, WithApprovalOf};
use sp_arithmetic::PerThing;
use sp_std::prelude::*;
/// Converts a vector of ratio assignments into ones with absolute budget value.
@@ -33,7 +30,6 @@ pub fn assignment_ratio_to_staked<A: IdentifierT, P: PerThing128, FS>(
) -> Vec<StakedAssignment<A>>
where
for<'r> FS: Fn(&'r A) -> VoteWeight,
ExtendedBalance: From<InnerOf<P>>,
{
ratios
.into_iter()
@@ -51,7 +47,6 @@ pub fn assignment_ratio_to_staked_normalized<A: IdentifierT, P: PerThing128, FS>
) -> Result<Vec<StakedAssignment<A>>, Error>
where
for<'r> FS: Fn(&'r A) -> VoteWeight,
ExtendedBalance: From<InnerOf<P>>,
{
let mut staked = assignment_ratio_to_staked(ratio, &stake_of);
staked
@@ -68,24 +63,19 @@ where
/// Note that this will NOT attempt at normalizing the result.
pub fn assignment_staked_to_ratio<A: IdentifierT, P: PerThing>(
staked: Vec<StakedAssignment<A>>,
) -> Vec<Assignment<A, P>>
where
ExtendedBalance: From<InnerOf<P>>,
{
) -> Vec<Assignment<A, P>> {
staked.into_iter().map(|a| a.into_assignment()).collect()
}
/// Same as [`assignment_staked_to_ratio`] and try and do normalization.
pub fn assignment_staked_to_ratio_normalized<A: IdentifierT, P: PerThing128>(
staked: Vec<StakedAssignment<A>>,
) -> Result<Vec<Assignment<A, P>>, Error>
where
ExtendedBalance: From<InnerOf<P>>,
{
) -> Result<Vec<Assignment<A, P>>, Error> {
let mut ratio = staked.into_iter().map(|a| a.into_assignment()).collect::<Vec<_>>();
ratio.iter_mut().map(|a|
a.try_normalize().map_err(|err| Error::ArithmeticError(err))
).collect::<Result<_, _>>()?;
ratio
.iter_mut()
.map(|a| a.try_normalize().map_err(|err| Error::ArithmeticError(err)))
.collect::<Result<_, _>>()?;
Ok(ratio)
}