Skip to content

Commit

Permalink
Add s utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Nov 10, 2023
1 parent 84ddbaf commit b04cd26
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions substrate/frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize>(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.
Expand Down Expand Up @@ -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};
Expand All @@ -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,
Expand Down

0 comments on commit b04cd26

Please sign in to comment.