From 14226419ee6a00aaa395e1e8c9ae299054a786e1 Mon Sep 17 00:00:00 2001 From: Daniel Olano Date: Fri, 10 Nov 2023 17:27:22 +0100 Subject: [PATCH 1/3] Add `s` utility function --- substrate/primitives/runtime/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/substrate/primitives/runtime/src/lib.rs b/substrate/primitives/runtime/src/lib.rs index 0e1d4c31fd71..899c9a39b9b0 100644 --- a/substrate/primitives/runtime/src/lib.rs +++ b/substrate/primitives/runtime/src/lib.rs @@ -954,6 +954,23 @@ pub fn print(print: impl traits::Printable) { print.print(); } +/// Utility function to declare string literals backed by an array of length N. +/// The input can be shorter than N, in that case the end of the array is padded with zeros. +/// +/// [`str_array`] is useful when converting strings that end up in the storage as fixed size arrays +/// or in const contexts where static data types have strings that could also end up in the storage. +pub const fn str_array(s: &str) -> [u8; N] { + debug_assert!(s.len() <= N, "String literal doesn't fit in array"); + let mut i = 0; + let mut arr = [0; N]; + let s = s.as_bytes(); + while i < s.len() { + arr[i] = s[i]; + i += 1; + } + arr +} + /// Describes on what should happen with a storage transaction. pub enum TransactionOutcome { /// Commit the transaction. From f30ba3896e3538c5cd49eaf69984292daf7ef8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 11 Nov 2023 17:19:39 +0100 Subject: [PATCH 2/3] Update substrate/primitives/runtime/src/lib.rs Co-authored-by: Davide Galassi --- substrate/primitives/runtime/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/primitives/runtime/src/lib.rs b/substrate/primitives/runtime/src/lib.rs index 899c9a39b9b0..4b060d65bdd8 100644 --- a/substrate/primitives/runtime/src/lib.rs +++ b/substrate/primitives/runtime/src/lib.rs @@ -955,6 +955,7 @@ pub fn print(print: impl traits::Printable) { } /// Utility function to declare string literals backed by an array of length N. +/// /// The input can be shorter than N, in that case the end of the array is padded with zeros. /// /// [`str_array`] is useful when converting strings that end up in the storage as fixed size arrays From 671eec1d43d02d14d5e3e5ba9821c452774d599a Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Sat, 11 Nov 2023 20:01:16 +0100 Subject: [PATCH 3/3] Update substrate/primitives/runtime/src/lib.rs --- substrate/primitives/runtime/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/substrate/primitives/runtime/src/lib.rs b/substrate/primitives/runtime/src/lib.rs index 4b060d65bdd8..ddf92554c830 100644 --- a/substrate/primitives/runtime/src/lib.rs +++ b/substrate/primitives/runtime/src/lib.rs @@ -960,6 +960,14 @@ pub fn print(print: impl traits::Printable) { /// /// [`str_array`] is useful when converting strings that end up in the storage as fixed size arrays /// or in const contexts where static data types have strings that could also end up in the storage. +/// +/// # Example +/// +/// ```rust +/// # use sp_runtime::str_array; +/// const MY_STR: [u8; 6] = str_array("data"); +/// assert_eq!(MY_STR, *b"data\0\0"); +/// ``` pub const fn str_array(s: &str) -> [u8; N] { debug_assert!(s.len() <= N, "String literal doesn't fit in array"); let mut i = 0;