Move bounded type definitions to sp-runtime (#11645)

* Move bounded type definitions to sp-runtime

* cargo fmt

* Fix compile error

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Move TryCollect to sp-runtime

* Write some docs

* Import missing types

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Keith Yeung
2022-06-13 14:31:42 +02:00
committed by GitHub
parent 8c1865d2f2
commit 2d6b0ecc21
13 changed files with 2484 additions and 2323 deletions
+43
View File
@@ -55,6 +55,7 @@ use sp_std::prelude::*;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
pub mod bounded;
pub mod curve;
pub mod generic;
pub mod legacy;
@@ -69,6 +70,9 @@ pub mod transaction_validity;
pub use crate::runtime_string::*;
// Re-export bounded types
pub use bounded::{BoundedBTreeMap, BoundedBTreeSet, BoundedSlice, BoundedVec, WeakBoundedVec};
// Re-export Multiaddress
pub use multiaddress::MultiAddress;
@@ -825,6 +829,45 @@ macro_rules! assert_eq_error_rate {
};
}
/// Build a bounded vec from the given literals.
///
/// The type of the outcome must be known.
///
/// Will not handle any errors and just panic if the given literals cannot fit in the corresponding
/// bounded vec type. Thus, this is only suitable for testing and non-consensus code.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! bounded_vec {
($ ($values:expr),* $(,)?) => {
{
$crate::sp_std::vec![$($values),*].try_into().unwrap()
}
};
( $value:expr ; $repetition:expr ) => {
{
$crate::sp_std::vec![$value ; $repetition].try_into().unwrap()
}
}
}
/// Build a bounded btree-map from the given literals.
///
/// The type of the outcome must be known.
///
/// Will not handle any errors and just panic if the given literals cannot fit in the corresponding
/// bounded vec type. Thus, this is only suitable for testing and non-consensus code.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! bounded_btree_map {
($ ( $key:expr => $value:expr ),* $(,)?) => {
{
$crate::traits::TryCollect::<$crate::BoundedBTreeMap<_, _, _>>::try_collect(
$crate::sp_std::vec![$(($key, $value)),*].into_iter()
).unwrap()
}
};
}
/// Simple blob to hold an extrinsic without committing to its format and ensure it is serialized
/// correctly.
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode, TypeInfo)]