diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index a01f3a01593ad..038f9e1bf3bcb 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -117,12 +117,27 @@ pub const LOG_TARGET: &str = "runtime::frame-support"; #[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] pub enum Never {} +/// Utility function to declare string literals backed by an array of length N. +/// The input can be shorter than its underlaying array in which case the end of the array is padded +/// with zeros. It can be used in const contexts. +pub const fn s(s: &str) -> [u8; N] { + debug_assert!(s.len() <= N, "String literal doesn't fit in array"); + let mut i = 0; + let mut a = [0; N]; + let s = s.as_bytes(); + while i < s.len() { + a[i] = s[i]; + i += 1; + } + a +} + /// A pallet identifier. These are per pallet and should be stored in a registry somewhere. #[derive(Clone, Copy, Eq, PartialEq, Encode, Decode, TypeInfo)] pub struct PalletId(pub [u8; 8]); impl TypeId for PalletId { - const TYPE_ID: [u8; 4] = *b"modl"; + const TYPE_ID: [u8; 4] = s("modl"); } /// Generate a [`#[pallet::storage]`](pallet_macros::storage) alias outside of a pallet. @@ -820,7 +835,7 @@ pub use macro_magic; pub mod testing_prelude { pub use super::{ assert_err, assert_err_ignore_postinfo, assert_err_with_weight, assert_error_encoded_size, - assert_noop, assert_ok, assert_storage_noop, parameter_types, traits::Get, + assert_noop, assert_ok, assert_storage_noop, parameter_types, s, traits::Get, }; pub use sp_arithmetic::assert_eq_error_rate; pub use sp_runtime::{bounded_btree_map, bounded_vec}; @@ -833,7 +848,7 @@ pub mod pallet_prelude { dispatch::{DispatchClass, DispatchResult, DispatchResultWithPostInfo, Parameter, Pays}, ensure, inherent::{InherentData, InherentIdentifier, ProvideInherent}, - storage, + s, storage, storage::{ bounded_btree_map::BoundedBTreeMap, bounded_btree_set::BoundedBTreeSet,