mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 07:58:02 +00:00
Add s utility function to frame support (#2275)
A utility function I consider quite useful to declare string literals that are backed by an array. --------- Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: Davide Galassi <davxy@datawok.net>
This commit is contained in:
@@ -954,6 +954,32 @@ 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.
|
||||
///
|
||||
/// # 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<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 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<R> {
|
||||
/// Commit the transaction.
|
||||
|
||||
Reference in New Issue
Block a user