Make Get<T> const friendly + Clean the runtime files a bit. (#6132)

* Make Get const friendly

* Better doc

* Grumble

* Better doc

* Clean runtime files more
This commit is contained in:
Kian Paimani
2020-05-27 14:40:07 +02:00
committed by GitHub
parent bba5d7d5da
commit 8279ba96df
11 changed files with 103 additions and 35 deletions
+1 -1
View File
@@ -110,7 +110,7 @@ impl frame_system::Trait for Test {
type OnKilledAccount = ();
}
parameter_types! {
pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
}
impl pallet_scheduler::Trait for Test {
type Event = Event;
+1 -1
View File
@@ -232,7 +232,7 @@ impl staking::Trait for Test {
}
parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}
impl offences::Trait for Test {
@@ -187,7 +187,7 @@ impl pallet_im_online::Trait for Test {
}
parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}
impl pallet_offences::Trait for Test {
+1 -1
View File
@@ -122,7 +122,7 @@ impl frame_system::Trait for Runtime {
}
parameter_types! {
pub const OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * MaximumBlockWeight::get();
}
impl Trait for Runtime {
+1 -1
View File
@@ -515,7 +515,7 @@ mod tests {
type Event = ();
}
parameter_types! {
pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
}
impl Trait for Test {
type Event = ();
+51 -4
View File
@@ -84,27 +84,62 @@ pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};
#[derive(Debug)]
pub enum Never {}
/// Macro for easily creating a new implementation of the `Get` trait. Use similarly to
/// how you would declare a `const`:
/// Macro for easily creating a new implementation of the `Get` trait. If `const` token is used, the
/// rhs of the expression must be `const`-only, and get is implemented as `const`:
///
/// ```no_compile
/// ```
/// # use frame_support::traits::Get;
/// # use frame_support::parameter_types;
/// // This function cannot be used in a const context.
/// fn non_const_expression() -> u64 { 99 }
///
/// const FIXED_VALUE: u64 = 10;
/// parameter_types! {
/// pub const Argument: u64 = 42;
/// pub const Argument: u64 = 42 + FIXED_VALUE;
/// pub OtherArgument: u64 = non_const_expression();
/// }
///
/// trait Config {
/// type Parameter: Get<u64>;
/// type OtherParameter: Get<u64>;
/// }
///
/// struct Runtime;
/// impl Config for Runtime {
/// type Parameter = Argument;
/// type OtherParameter = OtherArgument;
/// }
/// ```
///
/// Invalid example:
///
/// ```compile_fail
/// # use frame_support::traits::Get;
/// # use frame_support::parameter_types;
/// // This function cannot be used in a const context.
/// fn non_const_expression() -> u64 { 99 }
///
/// parameter_types! {
/// pub const Argument: u64 = non_const_expression();
/// }
/// ```
#[macro_export]
macro_rules! parameter_types {
(
$( #[ $attr:meta ] )*
$vis:vis const $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!{IMPL_CONST $name , $type , $value}
$crate::parameter_types!{ $( $rest )* }
);
(
$( #[ $attr:meta ] )*
$vis:vis $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
@@ -112,6 +147,18 @@ macro_rules! parameter_types {
$crate::parameter_types!{ $( $rest )* }
);
() => ();
(IMPL_CONST $name:ident , $type:ty , $value:expr) => {
impl $name {
pub const fn get() -> $type {
$value
}
}
impl<I: From<$type>> $crate::traits::Get<I> for $name {
fn get() -> I {
I::from($value)
}
}
};
(IMPL $name:ident , $type:ty , $value:expr) => {
impl $name {
pub fn get() -> $type {
+1 -1
View File
@@ -1877,7 +1877,7 @@ pub(crate) mod tests {
pub const MaximumExtrinsicWeight: Weight = 768;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MaximumBlockLength: u32 = 1024;
pub const Version: RuntimeVersion = RuntimeVersion {
pub Version: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("test"),
impl_name: sp_version::create_runtime_str!("system-test"),
authoring_version: 1,