-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Using array-bytes
for Array/Bytes/Hex Operation
#12111
Comments
Can you elaborate? |
Sorry, let me elaborate on this. There is no performance optimization. But this crate makes the developer's life much easier (IMO). It is completely no-std. So, you don't need to write something like It provides a lot of handy functions. For example, the // better error handling
let hash = array_bytes::hex_into::<H256, _>("0x840cff528f0a427cf1405cc474c76b369890fc01951c73c71ae1e2ad1e8baa7d").expect("valid hex; qed");
// also provides an infallible method
let hash = array_bytes::hex_into_unchecked::<H256, _>("0x840cff528f0a427cf1405cc474c76b369890fc01951c73c71ae1e2ad1e8baa7d");
// For `AccountId32`
let account = array_bytes::hex_into::<AccountId32, _>("0x840cff528f0a427cf1405cc474c76b369890fc01951c73c71ae1e2ad1e8baa7d").expect("valid hex; qed");
let account = array_bytes::slice_into::<AccountId32, _>(&[100, 118, 109, 58, 0, 0, 0, 0, 0, 0, 0, 213, 244, 148, 7, 4, 235, 76, 229, 228, 181, 24, 119, 212, 155, 88, 163, 233, 53, 49, 182, 96]).expect("valid data; qed"); And it supports a dynamic And in the runtime development, we work with the rust array/slice/vec every day. It provides some functions to transform the dynamic type into a fixed length array, example. Furthermore, if you are working with offchain-worker or genesis data. You might need to interact with the JSON object. It provides some useful deserializing functions. For example: "header": {
"block_number": "0x123"
}
#[derive(Debug, PartialEq, Deserialize)]
struct Header {
#[serde(deserialize_with = "array_bytes::de_hex2num")]
block_number: u32,
} So, I hope Substrate could use this crate. And let more Substrate developers know/enjoy such convenient functions/tools. |
I'm fine with switching to this crate. |
https://github.com/hack-ink/array-bytes/blob/38fb70e60306f2d6096bc2453fbcec150c59237c/src/lib.rs#L243 - Shouldn't these unchecked functions be marked as unsafe functions? I should not be able to call them from safe code because they are not safe. |
In https://paritytech.github.io/substrate/master/sc_service/index.html?search=unchecked, there are also a lot of unchecked functions. IMHO, But any discussion is welcome. |
This function is not really unchecked as @gilescope is saying. You are using there |
For this context (hex conversion), |
Thanks for the code review. |
Would make sense to me to have SR Labs or someone else audit the crate before we switch to it? |
I'm looking forward to this. |
Hi! I'm the author of array-bytes.
This crate is completely optimized for Substrate development.
It's pretty useful and easy to use. And it's completely no-std.
Did you consider replacing
hex
crate,HexDisplay
struct, and any otherarray
/bytes
/hex
operation with this crate?The text was updated successfully, but these errors were encountered: