sp-std removal from substrate/primitives (#3274)

This PR removes sp-std crate from substrate/primitives sub-directories.

For now crates that have `pub use` of sp-std or export macros that would
necessitate users of the macros to `extern crate alloc` have been
excluded from this PR.

There should be no breaking changes in this PR.

---------

Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
Squirrel
2024-03-18 05:29:35 +00:00
committed by GitHub
parent 6b1179f13b
commit 1b5f4243d1
110 changed files with 254 additions and 278 deletions
@@ -18,6 +18,7 @@
//! Structs and helpers for distributing a voter's stake among various winners.
use crate::{ExtendedBalance, IdentifierT, PerThing128};
use alloc::vec::Vec;
#[cfg(feature = "serde")]
use codec::{Decode, Encode};
use sp_arithmetic::{
@@ -25,7 +26,6 @@ use sp_arithmetic::{
Normalizable, PerThing,
};
use sp_core::RuntimeDebug;
use sp_std::vec::Vec;
/// A voter's stake assignment among a set of targets, represented as ratios.
#[derive(RuntimeDebug, Clone, Default)]
@@ -27,8 +27,8 @@
//! See [`balance`] for more information.
use crate::{BalancingConfig, Edge, ExtendedBalance, IdentifierT, Voter};
use alloc::vec::Vec;
use sp_arithmetic::traits::Zero;
use sp_std::prelude::*;
/// Balance the weight distribution of a given `voters` at most `iterations` times, or up until the
/// point where the biggest difference created per iteration of all stakes is `tolerance`. If this
@@ -18,8 +18,8 @@
//! Helper methods for npos-elections.
use crate::{Assignment, Error, IdentifierT, PerThing128, StakedAssignment, VoteWeight};
use alloc::vec::Vec;
use sp_arithmetic::PerThing;
use sp_std::prelude::*;
/// Converts a vector of ratio assignments into ones with absolute budget value.
///
@@ -74,15 +74,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
use alloc::{collections::btree_map::BTreeMap, rc::Rc, vec, vec::Vec};
use codec::{Decode, Encode, MaxEncodedLen};
use core::{cell::RefCell, cmp::Ordering};
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sp_arithmetic::{traits::Zero, Normalizable, PerThing, Rational128, ThresholdOrd};
use sp_core::{bounded::BoundedVec, RuntimeDebug};
use sp_std::{
cell::RefCell, cmp::Ordering, collections::btree_map::BTreeMap, prelude::*, rc::Rc, vec,
};
#[cfg(test)]
mod mock;
@@ -198,7 +199,7 @@ impl ElectionScore {
}
}
impl sp_std::cmp::Ord for ElectionScore {
impl core::cmp::Ord for ElectionScore {
fn cmp(&self, other: &Self) -> Ordering {
// we delegate this to the lexicographic cmp of slices`, and to incorporate that we want the
// third element to be minimized, we swap them.
@@ -210,7 +211,7 @@ impl sp_std::cmp::Ord for ElectionScore {
}
}
impl sp_std::cmp::PartialOrd for ElectionScore {
impl core::cmp::PartialOrd for ElectionScore {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
@@ -278,8 +279,8 @@ impl<AccountId: Clone> Edge<AccountId> {
}
#[cfg(feature = "std")]
impl<A: IdentifierT> sp_std::fmt::Debug for Edge<A> {
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
impl<A: IdentifierT> core::fmt::Debug for Edge<A> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Edge({:?}, weight = {:?})", self.who, self.weight)
}
}
@@ -299,7 +300,7 @@ pub struct Voter<AccountId> {
#[cfg(feature = "std")]
impl<A: IdentifierT> std::fmt::Debug for Voter<A> {
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Voter({:?}, budget = {}, edges = {:?})", self.who, self.budget, self.edges)
}
}
@@ -19,12 +19,12 @@
#![cfg(test)]
use alloc::collections::btree_map::BTreeMap;
use sp_arithmetic::{
traits::{One, SaturatedConversion, Zero},
PerThing,
};
use sp_runtime::assert_eq_error_rate;
use sp_std::collections::btree_map::BTreeMap;
use crate::{seq_phragmen, Assignment, ElectionResult, ExtendedBalance, PerThing128, VoteWeight};
@@ -131,7 +131,7 @@ where
if let Some(winner) = candidates
.iter_mut()
.filter(|c| !c.elected)
.min_by(|x, y| x.score.partial_cmp(&y.score).unwrap_or(sp_std::cmp::Ordering::Equal))
.min_by(|x, y| x.score.partial_cmp(&y.score).unwrap_or(core::cmp::Ordering::Equal))
{
winner.elected = true;
for n in &mut voters {
@@ -226,10 +226,10 @@ where
if backing_backed_stake.len() > 0 {
let max_stake = backing_backed_stake
.iter()
.max_by(|x, y| x.partial_cmp(&y).unwrap_or(sp_std::cmp::Ordering::Equal))
.max_by(|x, y| x.partial_cmp(&y).unwrap_or(core::cmp::Ordering::Equal))
.expect("vector with positive length will have a max; qed");
let min_stake = backed_stakes_iter
.min_by(|x, y| x.partial_cmp(&y).unwrap_or(sp_std::cmp::Ordering::Equal))
.min_by(|x, y| x.partial_cmp(&y).unwrap_or(core::cmp::Ordering::Equal))
.expect("iterator with positive length will have a min; qed");
difference = max_stake - min_stake;
@@ -254,7 +254,7 @@ where
support_map
.get(&x.0)
.and_then(|x| support_map.get(&y.0).and_then(|y| x.total.partial_cmp(&y.total)))
.unwrap_or(sp_std::cmp::Ordering::Equal)
.unwrap_or(core::cmp::Ordering::Equal)
});
let mut cumulative_stake = 0.0;
@@ -17,7 +17,8 @@
//! (very) Basic implementation of a graph node used in the reduce algorithm.
use sp_std::{cell::RefCell, fmt, prelude::*, rc::Rc};
use alloc::{rc::Rc, vec::Vec};
use core::{cell::RefCell, fmt};
/// The role that a node can accept.
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
@@ -49,8 +50,8 @@ impl<A> NodeId<A> {
}
#[cfg(feature = "std")]
impl<A: fmt::Debug> sp_std::fmt::Debug for NodeId<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> sp_std::fmt::Result {
impl<A: fmt::Debug> fmt::Debug for NodeId<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Node({:?}, {:?})",
@@ -24,12 +24,12 @@ use crate::{
balancing, setup_inputs, BalancingConfig, CandidatePtr, ElectionResult, ExtendedBalance,
IdentifierT, PerThing128, VoteWeight, Voter,
};
use alloc::vec::Vec;
use sp_arithmetic::{
helpers_128bit::multiply_by_rational_with_rounding,
traits::{Bounded, Zero},
Rational128, Rounding,
};
use sp_std::prelude::*;
/// The denominator used for loads. Since votes are collected as u64, the smallest ratio that we
/// might collect is `1/approval_stake` where approval stake is the sum of votes. Hence, some number
@@ -25,8 +25,8 @@ use crate::{
balance, setup_inputs, BalancingConfig, CandidatePtr, ElectionResult, ExtendedBalance,
IdentifierT, PerThing128, VoteWeight, Voter,
};
use alloc::{rc::Rc, vec, vec::Vec};
use sp_arithmetic::{traits::Bounded, PerThing, Rational128};
use sp_std::{prelude::*, rc::Rc};
/// Execute the phragmms method.
///
@@ -232,8 +232,8 @@ pub(crate) fn apply_elected<AccountId: IdentifierT>(
mod tests {
use super::*;
use crate::{Assignment, ElectionResult};
use alloc::rc::Rc;
use sp_runtime::{Perbill, Percent};
use sp_std::rc::Rc;
#[test]
fn basic_election_manual_works() {
@@ -26,8 +26,8 @@ use crate::{
Candidate, CandidatePtr, Edge, ExtendedBalance, IdentifierT, Support, SupportMap, Supports,
VoteWeight, Voter,
};
use alloc::{collections::btree_map::BTreeMap, rc::Rc, vec::Vec};
use sp_arithmetic::{traits::Zero, Perbill};
use sp_std::{collections::btree_map::BTreeMap, rc::Rc, vec::Vec};
/// The type used as the threshold.
///
/// Just some reading sugar; Must always be same as [`ExtendedBalance`];
@@ -51,11 +51,12 @@ use crate::{
node::{Node, NodeId, NodeRef, NodeRole},
ExtendedBalance, IdentifierT, StakedAssignment,
};
use sp_arithmetic::traits::{Bounded, Zero};
use sp_std::{
use alloc::{
collections::btree_map::{BTreeMap, Entry::*},
prelude::*,
vec,
vec::Vec,
};
use sp_arithmetic::traits::{Bounded, Zero};
/// Map type used for reduce_4. Can be easily swapped with HashMap.
type Map<A> = BTreeMap<(A, A), A>;
@@ -18,8 +18,8 @@
//! Traits for the npos-election operations.
use crate::ExtendedBalance;
use core::{fmt::Debug, ops::Mul};
use sp_arithmetic::PerThing;
use sp_std::{fmt::Debug, ops::Mul, prelude::*};
/// an aggregator trait for a generic type of a voter/target identifier. This usually maps to
/// substrate's account id.